【问题标题】:Set another value while binding path value is null in textblock在文本块中绑定路径值为空时设置另一个值
【发布时间】:2013-06-08 06:51:28
【问题描述】:

我有一个文本块,在我的源 2 属性中,用于在文本块中显示名称 Property1 为 null 然后显示 Property2

<TextBlock x:Name="txtName" Text="{Binding Property1 , Mode=OneWay, Converter={StaticResource DataConvertor}, ConverterParameter=lblDisplayName,TargetNullValue={Binding Path=Property ,Mode=OneWay}}"></TextBlock>

但它在运行时给我错误

【问题讨论】:

    标签: c# silverlight binding


    【解决方案1】:

    通过查看这段代码,很难说出你在这里想要做什么。

    但我建议你处理这个是你的 ViewModel

        private string _property1;
        public string Property1
        {
                get{
                    return _property1;
                }
                set
                {
                    Property1 = value;
                    OnPropertyChanged("Property1");
                }
        }
    
        private string _property2;
        public string Property2
        {
                get{
                    return _property2;
                }
                set
                {
                    if(value!=null)
                    {
                        Property2 = value;
                    }else
                    {
                        Property2 = Property1;
                    }                   
                    OnPropertyChanged("Property2");
                }
        }
    

    【讨论】:

    • 感谢您的回答,但我认为使用转换器类分配第二个属性而不是更改属性值
    【解决方案2】:

    我得到了答案,但方式不同

    <TextBlock x:Name="txtName" Text="{Binding  Mode=OneWay, Converter={StaticResource DataConvertor}, ConverterParameter=lblName}" VerticalAlignment="Bottom" FontWeight="Bold" ></TextBlock>
    

    我没有为绑定分配任何特定属性并从转换器分配值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多