Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.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# 如何更改itemsControl stackpanel背景色_C#_Wpf_Itemscontrol_Stackpanel - Fatal编程技术网

C# 如何更改itemsControl stackpanel背景色

C# 如何更改itemsControl stackpanel背景色,c#,wpf,itemscontrol,stackpanel,C#,Wpf,Itemscontrol,Stackpanel,如果我有一个包含10个项目的列表someList,我将通过itemsControl在我的页面上显示它们,如下所示: <ItemsControl DataContext="{Binding [someViewModel]}" BorderBrush="Black" ItemSource="{Binding someList}"> <ItemsControl.ItemTemplate> <

如果我有一个包含10个项目的列表
someList
,我将通过
itemsControl
在我的页面上显示它们,如下所示:

<ItemsControl DataContext="{Binding [someViewModel]}" 
              BorderBrush="Black" 
              ItemSource="{Binding someList}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Border BorderThickness="1" Background="Green">
                <StackPanel MouseDown="{Binding Path=DataContext.someCommand,   
                                        RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ItemsControl}}}" 
                                        Command Parameter="{Binding someID}">
                    <TextBlock Text="{Binding something}">
                </StackPanel>
            </Border>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>


我能够触发
someCommand
方法,并且能够将
someID
作为输入参数传入。现在我想知道如何更新stackPanel背景色,使其看起来像“选中的”。这意味着现在所有项目都将有一个绿色背景,当我单击其中一个stackpanel时,该stackpanel应将背景更改为红色,并将其他项目更改为绿色

为什么不这样做

<DataTemplate>
    <Border BorderThickness="1" Background="Green" x:Name="MyBorder">
        <StackPanel MouseDown="{Binding Path=DataContext.someCommand,   
            RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ItemsControl}}}" 
            Command Parameter="{Binding someID}">
            <TextBlock Text="{Binding something}">
        </StackPanel>
    </Border>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding Path=IsSelected}" Value="true">
                    <Setter TargetName="MyBorder" Property="Background" Value="Black" />
            </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

如果您想使用
项目控件
,您可以使用自定义
控件模板将
项目模板
更改为
单选按钮
,该控件模板将包括
边框
,当
被选中==true时,
背景
将变为红色

<ItemsControl DataContext="{Binding [someViewModel]}" BorderBrush="Black" ItemSource="{Binding someList}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <RadioButton Content="{Binding something}" GroupName="radioGroup">
                <RadioButton.Template>
                    <ControlTemplate TargetType="{x:Type RadioButton}">
                        <Border Background="Green" x:Name="PART_Border">
                            <ContentPresenter/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter TargetName="PART_Border" Property="Background" Value="Red"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </RadioButton.Template>
            </RadioButton>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
甚至可以这样做,而无需更改
模板

<ListBox DataContext="{Binding [someViewModel]}" BorderBrush="Black" ItemSource="{Binding someList}">
    <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
    </ListBox.Resources>
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Background" Value="Green"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>


在WPF中,通常更容易选择一个具有您需要的功能的控件,并将其样式设置为您想要的样式,然后以相反的方式执行此操作

为什么不将
ItemTemplate
更改为一个组中的
RadioButton
,并将其样式设置为选中/取消选中时,或者最好使用
ListBox
和单个选择和设置样式?或者使用ListView和dataTemplate。你的方法行不通。我猜这是因为
MyBorder
不是唯一的?它不是这样工作的-MyBorder在datatemplate的范围内是唯一的(这是一个封装边界),所以它可以像我所展示的那样工作,您需要对绑定到的数据项使用IsSelected属性(例如textblock绑定到'something'属性。viewmodel中的命令需要在正确的数据项上设置'IsSelected'。
<ListBox DataContext="{Binding [someViewModel]}" BorderBrush="Black" ItemSource="{Binding someList}">
    <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
    </ListBox.Resources>
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Background" Value="Green"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>