1

我有以下 xaml 代码:

<ControlTemplate x:Key="ViewItemTemplate"
                         TargetType="ListViewItem">
        <StackPanel Orientation="Horizontal">
            <CheckBox Margin="0,0,3,0" x:Name="CkBox">
                <CkBox.IsChecked>
                    <Binding Path="IsSelected"
                                 Mode="TwoWay">
                        <Binding.RelativeSource>
                            <RelativeSource Mode="TemplatedParent" />
                        </Binding.RelativeSource>
                    </Binding>
                </CkBox.IsChecked>
                 <DataTrigger Binding="{Binding InvalidForeground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" Value="true">
                    <Setter TargetName="CkBoxVisual" Property="Foreground" Value="#999999"/>
                </DataTrigger>
            </CheckBox>
            <ContentPresenter />
        </StackPanel>
    </ControlTemplate>

Public class Demo
{
  private bool _invalidForeGround;
        public bool InvalidForeground
        {
            get
            { return _invalidForeGround; }
            set
            {
                _invalidForeGround = value;
                Notify("InvalidForeground");
            }
        }
        private void Notify(string propName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
        public event PropertyChangedEventHandler PropertyChanged;
}

Xaml.cs 文件:

private readonly RangeObservableCollection<SlotInfo> _validDemo;
public ReadOnlyObservableCollection<SlotInfo> DemoChecks
        {
            get { return ( ReadOnlyObservableCollection<SlotInfo> )GetValue( DemoChecksProperty ); }
            set
            {
                SetValue( DemoChecksProperty, value );
                this.CheckInvariants();
            }
        }

        foreach(var demo in DemoChecks)
        { 
               demo.InvalidForeground = true;
               _validDemo.Add(demo);            
        }

从这段代码中,我希望复选框文本 Demo1、Demo2 和 Demo3 应该是红色的:

_
|_| Demo1  
 _
|_| Demo2
 _
|_| Demo3

But instead i get it like this:
 _
|_| System.Windows.DataTrigger Demo1  
 _
|_| System.Windows.DataTrigger Demo2
 _
|_| System.Windows.DataTrigger Demo3

谁能帮忙,我做错了什么?

4

0 回答 0