Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WPF中自定义列表框中选择的可视化_C#_Wpf_Templates_Xaml_Listbox - Fatal编程技术网

C# 在WPF中自定义列表框中选择的可视化

C# 在WPF中自定义列表框中选择的可视化,c#,wpf,templates,xaml,listbox,C#,Wpf,Templates,Xaml,Listbox,我有一个带有如下列表框的控件: <local:MyControl x:Name="LayoutRoot" ...> <local:MyControl.Resources> <ItemsPanelTemplate x:Key="myTemplate"> <StackPanel Background="Yellow"/> </ItemsPanelTemplate> </local:MyContro

我有一个带有如下列表框的控件:

<local:MyControl x:Name="LayoutRoot" ...>

  <local:MyControl.Resources>
    <ItemsPanelTemplate x:Key="myTemplate">
      <StackPanel Background="Yellow"/>
    </ItemsPanelTemplate>
  </local:MyControl.Resources>

  <Border>    
    ...
    <ListBox  ItemsPanel="{DynamicResource myTemplate}">
      <ListBox.ItemTemplate>
        <DataTemplate>
          <Rectangle Height="50" Width="200" Margin="10" Fill="Red"/>
        </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>
    ...
  </Border>
</local:MyControl>

...
...
这是我运行它时的样子:

列表框根据每个项目的状态在其周围绘制一个“框架”:

  • 未选择:透明
  • 选中并且列表框处于焦点:蓝色
  • 选中且列表框不对焦:白色
我猜“框架”是ListBoxItem背景的可见部分,位于矩形对象的边界下,但我不确定


我想为每个不同的状态定制“框架”-s的外观。你能给我一个例子吗?

你可以使用
ItemContainerStyle
,它在本例中适用于每个
ListBoxItem

<ItemsControl ...>
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <!-- go nuts here if you want -->
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>


当然,您可能不需要更改整个
ListBoxItem
模板-从您的问题中不清楚您实际上想要实现什么,所以我不能说。您只需更改默认模板所利用的其他属性就可以了。

如果您使用的是Expression Blend,请看如何操作

如果你没有使用Blend(像我一样),我记得有一段时间以前也遇到过类似的问题,我认为应用样式和模板(正确的方法)太费劲了,于是我想到了这个

<Style TargetType="{x:Type ListViewItem}">
     <Style.Resources>
         <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#00000000"/>
         <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#00000000"/>
     </Style.Resources>
</Style>

请注意,我使用的是ListView来代替ListBox。只需将其添加到最近的资源块中即可。在我的例子中是
另外,可以自由地尝试颜色。我尝试了一些在选择元素时不进行任何高亮度显示的方法

更新:尽管我在这里使用的是ListView,但我认为这种方法应该可以很好地用于ListBox。这是因为我已尝试在SystemColors对象而不是ListView上设置属性。使用列表框控件尝试此操作,如果有效,请发回。

您看到的不是“框架”,而是您在矩形中设置的边距的结果。 请注意,您不应该将矩形设置为DataTemplate,否则您将只看到。。。列表中的矩形。只需为此使用一个边框,您就可以在其中放置内容(使用

要获得正确的颜色,请在ItemContainerStyle中使用样式。问题是触发器 在“IsSelected”情况下不起作用,因为默认模板在选择时将背景颜色设置为蓝色,没有机会更改它。因此,您必须重新定义模板,使背景不可见,并在模板内定义触发器。你的风格应该有点像这样:

    <Style x:Key="myStyle" TargetType="ListBoxItem" >
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Border Background="{TemplateBinding Background}">
                        <Border Background="Red"  Margin="10">
                            <ContentControl Content="{Binding}" />
                        </Border>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="Black" />
                        </Trigger>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="Red" />
                        </Trigger>
                        <Trigger Property="IsFocused" Value="True">
                            <Setter Property="Background" Value="Orange" />
                        </Trigger>
                    </ControlTemplate.Triggers >
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

现在,您可以在列表框中使用以下样式:ItemContainerStyle=“{StaticResource myStyle}”

请注意,触发器的顺序会改变结果的颜色:最后一次赢(例如,对于选中的项目和鼠标悬停的项目)