![](/img/trans.png)
[英]TargetName property cannot be set on a Style Setter, so how is it set?
[英]How to set a nullable property value by Style setter?
我正在開發Windows Store(8.1)應用程序。
我有一個具有可為null的int屬性(類型為“ int?”)的控件,當我嘗試通過Style將其設置為int時,出現錯誤。
<Setter Property="Minimum" Value="0" />
Error Cannot assign to nullable type on property Minimum
有任何想法嗎?
以下示例描述了解決方案,請根據需要進行調整。
C#:
class IsNullToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value == null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
XAML:
<Window.Resources>
<local:IsNullToBoolConverter x:Key="IsNullToBoolConverter"/>
</Window.Resources>
<TextBlock Text="Text">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding Age, Converter={StaticResource IsNullToBoolConverter}}" Value="False">
<Setter Property="FontSize" Value="55"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
您可以通過使用TargetNullValue屬性來實現。 使用以下代碼,
TargetNullValue={x:Static sys:String.Empty}
注意 :
sys
是mscorlib中System的導入的xml名稱空間:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
希望這會有所幫助..
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.