-1

私は次のコードを持っています:

<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}" Value="true">
                                <Setter TargetName="CkBoxVisual" Property="Foreground" Value="#999999"/>
                            </DataTrigger>
                        </CheckBox>
                        <ContentPresenter />
                    </StackPanel>
            </ControlTemplate>

InvalidForeground をバインドするにはどうすればよいですか? DataTemplate を使用するように指示する多くの例をオンラインで調べました。しかし、StackPanel の上に DataTemplate を追加すると、エラーが発生しますか? 私は何か間違ったことをしていますか?

コードを追加できるように、InvalidForeground をバインドしようとしています。エラーが表示されます: DataContext が不明なため、シンボル 'InvalidForeground' を解決できません。

4

2 に答える 2

1

WPFアプリケーション内で使用するカスタマイズされたチェックボックスコントロールを宣言しようとしているようです。そのため、「InvalidForeground」プロパティは公開されますが、テンプレートは実際のタイプが何を期待しているかを理解していません。

カスタムボタンの完全なステップバイステップを提供する別の回答をここに投稿しました。原則は同じで、宣言や型などについての私の理解を指摘しようとしました。これだけでなく、他のクラステンプレートについても理解できることを願っています。

于 2012-11-20T15:49:56.267 に答える
0
<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 partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        abc A = new abc();
        A.InvalidForeground = true;
    }
}
public class abc : INotifyPropertyChanged
{
    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;
}

InvalidForegroundは、上記のコードがテンプレートであるコントロールのDataContextのプロパティである必要があります。これがお役に立てば幸いです。

于 2012-11-20T15:51:06.987 に答える