3

我有一个包含一些项目的列表框。当一个项目被选中时,我想改变 UserControlButton 的背景颜色。

我怎样才能做到这一点?

<Window.Resources>
    <Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    </Style>
</Window.Resources>

<Border x:Name="UserScrollContainer">
       <ListBox x:Name="UserContainer" ItemsSource="{Binding allUserViewModel.Users}" 
                             Background="Transparent"   
                             HorizontalContentAlignment="Stretch"
                             ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
                             ScrollViewer.VerticalScrollBarVisibility="Visible"
                             BorderThickness="0" Margin="0" Padding="0"    
                             ItemContainerStyle="{DynamicResource ListBoxItemStyle}">

            <ListBox.ItemTemplate>
                 <DataTemplate>
                      <local:UserControlButton x:Name="UserControlButton" />   
                                // Change background color depending if it is selected
                  </DataTemplate>
            </ListBox.ItemTemplate>
      </ListBox>
</Border>     

编辑

我知道我可以添加如下内容:

        <Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>

        <Setter Property="Background" Value="Lightblue"/>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="true">
                <Setter Property="Background" Value="Red"/>
            </Trigger>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter Property="Background" Value="Yellow"/>
            </Trigger>
        </Style.Triggers>
    </Style>

但后来我得到了这个结果:

在此处输入图像描述

我需要更改用户控件的背景,而不是列表框项的背景。

4

2 回答 2

6

你有几种方法来解决你的问题。我将在这里描述其中之一。

您可以在 , 上定义<Style /><UserContorl />反映ListBoxItem.IsSelected属性:

<DataTemplate>
    <local:UserControlButton x:Name="UserControlButton">
        <local:UserControlButton.Style>
            <Style TargetType="{x:Type local:UserControlButton}">
                <Setter Property="Background" Value="Lightblue"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}}" Value="true">
                        <Setter Property="Background" Value="Red"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </local:UserControlButton.Style>
    </local:UserControlButton>
</DataTemplate>
于 2013-11-13T16:36:53.283 回答
0

您必须将用户控件的数据上下文设置为等于所选项目。并且必须使用转换器将所选值转换为背景。

假设你有这样的用户控制:

    <UserControl x:Class="ArdonorDemonstration.UserControlButton" >
    <Button Content="{Binding}"
               x:Name="btnUC" Background="{Binding}"></Button>
</UserControl>

你必须设置

<ListBox.ItemTemplate> 
                <DataTemplate>
                    <local:UserControlButton x:Name="UserControlButton"
                                             DataContext="{Binding}" /> 
                </DataTemplate>

如果您想捕获所选项目,则 1. 在用户控件中公开一个依赖属性。2. 在 ListBox ItemTemplate 中将其设置为 SelectedItem。3. 在用户控件中,将按钮背景颜色设置为该依赖属性。

于 2013-11-13T16:47:58.817 回答