![](/img/trans.png)
[英]How do i set a Binding Value to a TargetNullValue property in a DataGridTextColumn WPF XAML
[英]How can I set the value using xaml only in WPF's Binding?
我有一个这样的自定义绑定:
public class MyBinding : Binding
{
public class ValueConverter : IValueConverter
{
public ValueConverter(string A)
{
this.A = A;
}
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ((bool)value == true)
{
return A;
}
else
{
return "another value";
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
public string A
{
get;
set;
}
}
public string A
{
get;
set;
}
public MyBinding()
{
this.Converter = new ValueConverter(A);
}
}
和XAML(IsEnable是类MainWindow的属性):
<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication5"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock>
<TextBlock.Text>
<local:MyBinding A="value" Path="IsEnable" RelativeSource="{RelativeSource AncestorType=Window, Mode=FindAncestor}"/>
</TextBlock.Text>
</TextBlock>
</Grid>
我愿意在IsEnable
为true时使TextBlock
显示A,在IsEnable
为false时显示another value
。
但是无论如何,我都无法在xaml中设置A的值。 调试时始终为null
。
我在某个地方错了吗?
在调用MyBinding的构造函数之后,将分配A
属性的值。
您可以在A
的设置器中创建Converter:
public class MyBinding : Binding
{
...
private string a;
public string A
{
get { return a; }
set
{
a = value;
Converter = new ValueConverter(a);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.