如何在WPF-C中进行绑定#

如何在WPF-C中进行绑定#,wpf,c#-4.0,Wpf,C# 4.0,我有以下代码: <ControlTemplate x:Key="ViewItemTemplate" TargetType="ListViewItem"> <StackPanel Orientation="Horizontal"> <CheckBox Margin="0,0,3,0" x:Name="CkBox">

我有以下代码:

<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>

我怎样才能绑定到地面?我在网上查找了很多他们告诉我使用DataTemplate的例子。但是当我在StackPanel上面添加DataTemplate时,我会出错吗?我做错什么了吗


我正在尝试绑定InvalidForeground,以便向其中添加一些代码。我收到一个错误:由于未知的DataContext,无法解析符号“InvalidForeground”

似乎您正试图声明一个自定义的复选框控件以在WPF应用程序中使用。因此,您的“InvalidForeground”属性将被公开,但模板不了解其实际类型

我发布了另一个答案,它给出了一个定制按钮的完整步骤。原则是一样的,我试着指出我对声明、类型等的理解。希望它不仅能指导您,还能指导您使用其他类模板


<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;
}
公共部分类主窗口:窗口 { 公共主窗口() { 初始化组件(); DataContext=this; abc A=新abc(); A.InvalidForeground=真; } } 公共类abc:INotifyPropertyChanged { 私人病院; 公共场所 { 得到 {返回invalidForeGround;} 设置 { invalidForeGround=值; 通知(“无效场所”); } } 私有void Notify(字符串propName) { if(PropertyChanged!=null) PropertyChanged(这是新PropertyChangedEventArgs(propName)); } 公共事件属性更改事件处理程序属性更改; }

InvalidForeground必须是控件的DataContext中的属性,上面的代码是该控件的模板。我希望这将有助于

您想实现什么?您能更具体一点吗?你在问什么?您遇到了什么错误?@DanielHilgarth-我正在尝试绑定InvalidForeground,以便向其中添加一些代码。我收到一个错误:由于未知的数据上下文,无法解析符号“InvalidForeground”。您正在尝试绑定
InvalidForeground
。但这是什么呢?它的定义是什么?“所以我可以给它添加一些代码”是什么意思?@DanielHilgarth-InvalidForeground只是一个可以判断是真是假的谎言。我想在代码中添加属性以获取该值,并在设计中更改复选框文本的ForegroundColor。我该怎么做?有什么想法吗?公共类abc{public bool InvalidForeground{get;set;}}那么我该如何进行更改?我想创建类abc的实例,并更改该实例的InvalidForeground颜色属性?您想在何处创建abc实例?更新了答案并指定了findparentI尝试将InvalidForeground绑定到类:abc的模式。我该怎么做?有什么想法吗?正如上面答案“这里”中的链接,它一步一步地显示出来。您拥有许多组件/属性,但请查看这些组件/属性,看看它是否对您有所帮助。您必须定义类类型的模板,以便它能够正确解析并知道它具有哪些属性,然后它应该找到它进行绑定。请在您有机会查看后告诉我。按钮模板比复选框更复杂,但我相信你会看到相似之处。