【问题标题】:Why doesnt the binding of a property work?为什么属性的绑定不起作用?
【发布时间】:2016-07-05 10:16:14
【问题描述】:

这是我的第一个问题,所以要小心:) 我正在尝试在xaml中绑定属性(a),前景色有效(因此datagrid单元格的参考是正确的),但背景不是,我试图理解为什么,如果我尝试调试我的属性,程序没有进入它......

(a)=
    public int CellGiorno1
    {
        get
        {
            int a = myfunctionexample(day, Username, month, year);
            return a;
            //return 0-1-2
        }
    }

(如果返回的数字为 1,我想要为背景着色的数据网格的列) DataGridTextColumn Header="2" x:Name="HeaderGG1" Binding={Binding Desc_GG1}" CellStyle="{StaticResource CellStyleGiorno}"/>

 (the style with the trigger that color the foreground but not the background)
    <Style x:Key="CellStyleGiorno" TargetType="DataGridCell">      
                <Setter Property="Foreground" Value="Red"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding CellGiorno1}" Value="1">
                        <Setter Property="Background" Value="Green"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>

【问题讨论】:

    标签: c# wpf xaml triggers styles


    【解决方案1】:

    这里的第一个问题是您的 get 和 set 方法。看看这个结构:

    class Name
    {
        private string _mynam = "";
    
        public string mynam
        {
            set
            {
                _mynam = value;
            }
            get
            {
                return _mynam;
            }
        }
    }
    

    你没有set方法,你在get方法中设置方法。

    【讨论】:

    • 我添加了 set 方法但它还不起作用,现在我正在尝试 mikeT 解决方案,但我无法访问日、用户名、月、年,因为现在我在另一个班级.. ..
    【解决方案2】:

    属性需要附加更改结构的通知才能启用绑定

    这通常使用 INotifyPropertyChanged 或 DependencyProperty 完成

    例如

    public class myClass :INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        public void ChangeCell()
        {
            PropertyChanged(this,new PropertyChangedEventArgs("CellGiorno1")
        }
        public int CellGiorno1
        {
            get
            {
                int a = myfunctionexample(day, Username, month, year);
                return a;
                //return 0-1-2
            }
        }
    }
    

    所以在这种情况下,调用 ChangeCell 会通知所有连接到 CellGiorno1 的绑定,它们需要获取 CellGiorno1 的值,因为它已更改

    【讨论】:

    • 我在这个软件公司工作了 1 个月,我已经看到了这些 INotifyPropertyChanged 或 DependencyProperty 但我认为这些是为了更新界面的某些内容......但这可能是答案,现在我看起来很小心!谢谢!如果是问题我会回答的
    • 你可能想阅读这个wpf-tutorial.com/data-binding/introduction它涵盖了绑定的所有基础
    • 我在那个网站上读了很多东西哈哈哈哈非常有用....我有它的基础,我已经完成了一些功能,比如带触发器的颜色,使用多触发器,但是触发一个功能现在是我的问题....
    • "该属性需要附有更改结构的通知" 我该怎么做?在这个软件公司的程序中,有一个类似我创建的其他绑定,但它调用了该属性,而我没有,但我没有找到任何引用它的 INotifyPropertyChanged 或 DependencyProperty
    • 但还要注意您要绑定的内容以及要绑定的内容,在您的代码中,您将 CellGiorno1 的值绑定到 DataTrigger 的名为“Binding”的依赖属性,而无需了解更多关于您的代码的信息我可以' 不是说这是正确的,但如果没有更改通知挂钩,绑定将永远不会获得 CellGiorno1 的值,因为它永远不会知道它的更改
    猜你喜欢
    • 1970-01-01
    • 2017-11-24
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    相关资源
    最近更新 更多