c# - 有人可以给我一些关于 WPF 的建议吗?

标签 c# wpf button styles

我正在制作一个数据绑定(bind)的用户控件。查询结果包括对象集合 (A),其中 A 具有其他结果集合 (B)。所以A包含多个B。

在用户控件中,我想将集合 A 表示为扩展器,将 B 表示为扩展器内的按钮。这是我得到的

<UserControl x:Class="GuideLib.ModuleControls.uclQuestions"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<ItemsControl x:Name="ictlAnswers" ItemsSource="{Binding}" Background="Gray">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Vertical"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Expander ExpandDirection="Down" Background="DarkGray">
                <Expander.Header>
                    <TextBlock Text="{Binding Path=Name}" Foreground="White"/>
                </Expander.Header>
                <ListBox x:Name="SubListBox" ItemsSource="{Binding Path=Question}" Background="Gray" Foreground="White" HorizontalAlignment="Stretch">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
                                <Button Content="{Binding Path=Name}" Margin="10,2,2,2" HorizontalAlignment="Stretch" Tag="{Binding Path=ID}">
                                    <Button.Style>
                                        <Style TargetType="{x:Type Button}">
                                            <Setter Property="Background" Value="Gray" />
                                            <Style.Triggers>
                                                <Trigger Property="IsMouseOver" Value="True">
                                                    <Setter Property="Background" Value="Orange" />
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Button.Style>
                                </Button>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </Expander>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

我有很多问题。

1: 我无法让扩展器中的按钮水平拉伸(stretch)

2:如何设置按钮的Tag属性为B的整个对象

3:为什么默认的mouseover效果还是会执行。

谢谢

最佳答案

好的:

1. 你需要在ListBoxStackPanel里面设置Horizo​​ntalContentAlignment="Stretch" DataTemplate 设置 Orientation="Vertical"

2.Button 上设置 Tag="{Binding .}"

3. 将您的 Button.Style 更新为如下内容:

<Button.Style>
  <Style TargetType="{x:Type Button}">
    <Setter Property="Background"
            Value="Gray" />
    <Setter Property="OverridesDefaultStyle"
            Value="True" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type Button}">
          <Border Background="{TemplateBinding Background}">
            <ContentPresenter />
          </Border>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
    <Style.Triggers>
      <Trigger Property="IsMouseOver"
                Value="True">
        <Setter Property="Background"
                Value="Orange" />
      </Trigger>
    </Style.Triggers>
  </Style>
</Button.Style>

最后,在添加问题 4. 之前,请记住 ListBoxItem 有自己的 Style。因此,要完全覆盖 MouseOver 上的效果,而不是在 Button 上,而是在“Item”中,您必须为 提供自定义 Style >ListBox.ItemContainerStyle

哦,开始使用 Snoop .它可以帮助您调试布局问题。认为您可以用它解决问题 1,因为它会向您显示 ListBoxItemContentPresenter 使用的是必需的宽度,而不是拉伸(stretch)。

关于c# - 有人可以给我一些关于 WPF 的建议吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16234043/

相关文章:

c# - 如何使用 xamarin.forms 在弹出窗口中创建表单?

c# - 在 C# - C++/CLI - C++ Windows 窗体应用程序退出之前跟踪 - 并正确结束 - native 和托管线程

c# - 第一次画面

c# - WPF Metro 窗口全屏

android - 如何在不旋转布局的情况下改变方向时旋转按钮?

jquery - PhoneGap构建+ jquerymobile : onclick on button does not work

c# - 在 MVC 中初始化基本 Controller 时

c# - ML.NET 二元分类模型不起作用

wpf - WPF 的自定义复选框样式

c++ - winapi中的多行按钮