【问题标题】:How to do binding in WPF - C#如何在 WPF 中进行绑定 - C#
【发布时间】:2012-11-20 15:33:58
【问题描述】:

我有以下代码:

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

【问题讨论】:

  • 你想达到什么目的?
  • 你能说得更具体点吗?你在问什么?你遇到了什么错误?
  • @DanielHilgarth - 我正在尝试绑定 InvalidForeground,以便我可以添加一些代码。我收到一个错误:由于未知的 DataContext,无法解析符号“InvalidForeground”。
  • 您正在尝试绑定InvalidForeground。但它是什么?它在哪里定义?你说的“所以我可以添加一些代码”是什么意思??
  • @DanielHilgarth - InvalidForeground 只是一个布尔值,它取真或假。我想在代码中添加属性以获取该值并更改设计中复选框文本的 ForegroundColor。

标签: wpf c#-4.0


【解决方案1】:

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

我发布了另一个答案here,它为自定义按钮提供了完整的逐步说明。原则是一样的,我试图指出我对声明、类型等的理解。希望它不仅可以指导您,还可以指导其他类模板。

【讨论】:

  • 我正在尝试将 InvalidForeground 绑定到类:abc 可以说。我怎样才能做到这一点?有什么想法吗?
  • 如上述答案“这里”中的链接,一步一步显示。你有很多组件/属性,但看看它,看看它是否对你有帮助。您必须定义类类型的模板,以便它可以正确解析并知道 IT 具有哪些属性,然后它应该找到它进行绑定。请在您有机会查看后告诉我。按钮模板比复选框更复杂,但我敢肯定你会看到相似之处。
【解决方案2】:
<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中的属性。希望对你有帮助

【讨论】:

  • 我正在尝试将 InvalidForeground 绑定到类:abc 可以说。我怎样才能做到这一点?有什么想法吗?
  • 公共类 abc { public bool InvalidForeground { get;放; }}
  • 那么我怎样才能做出这个改变呢?我想创建 abc 类的实例并更改该实例的 InvalidForeground 颜色属性?
  • 你想在哪里创建 abc 的实例?
  • 更新答案并指定模式 findparent
猜你喜欢
  • 2011-07-19
  • 1970-01-01
  • 1970-01-01
  • 2010-09-12
  • 2021-11-16
  • 1970-01-01
  • 2021-03-24
  • 2012-08-24
  • 2011-05-14
相关资源
最近更新 更多