[英]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.