wpf Treeview CheckBox项目ForeGround颜色在禁用时无法更改?

时间:2011-05-11 10:21:42

标签: wpf c#-4.0 binding

这是Treeview CheckBox Item的我的xaml样式。我正在使用Syncfusion Treeview。

<Style x:Key="contractListItemContainerStyle" TargetType="{x:Type syncfusion:TreeViewItemAdv}">
    <Setter Property="IsExpanded" Value="True" />
    <Setter Property="IsSelected" Value="{Binding IsInitiallySelected, Mode=OneTime}" />
    <Setter Property="KeyboardNavigation.AcceptsReturn" Value="True" />
    <Setter Property="IsEditable" Value="False" />

    <Setter Property="IsEnabled" Value="{Binding Enable}" />
    <Setter Property="Foreground" Value="Red" />
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="True">
            <Setter Property="Foreground" Value="Green" />
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Foreground" Value="Red" />
        </Trigger>
    </Style.Triggers>
</Style>

<HierarchicalDataTemplate x:Key="selectedContractsDataTemplate" ItemsSource="{Binding Children}" >
    <StackPanel Orientation="Horizontal">
        <CheckBox Margin="2,0,2,0"
             Focusable="False"
             IsChecked="{Binding Content.IsChecked}"
             VerticalAlignment="Center"  />
        <ContentPresenter Content="{Binding}" Margin="2,0"  />
    </StackPanel>
</HierarchicalDataTemplate>

如果我从ViewModel设置Enable为False,则Color不会变为红色但如果我设置为true,则会变为绿色。为什么? Pelase建议。

2 个答案:

答案 0 :(得分:1)

代码应该可以工作,请参阅/试试这个:

<StackPanel>
    <CheckBox Name="chk" Content="check this...">
        <CheckBox.Style>
            <Style>
                <Setter Property="CheckBox.Foreground" Value="Red" />
                <Style.Triggers>
                    <Trigger Property="CheckBox.IsEnabled" Value="True">
                        <Setter Property="CheckBox.Foreground" Value="Green" />
                    </Trigger>
                    <Trigger Property="CheckBox.IsEnabled" Value="False">
                        <Setter Property="CheckBox.Foreground" Value="Red" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </CheckBox.Style>
    </CheckBox>
    <Button Content="press" Click="Button_Click"/>
</StackPanel>

绑定可能有问题吗?

答案 1 :(得分:0)

试试这个

  <Setter Property = "Foreground" Value = "Red"/>
   <Style.Triggers>
       <Trigger Property="IsEnabled" Value="True">
             <Setter Property="Foreground" Value="Green" />
       </Trigger>
    </Style.Triggers>