读书人

App.xaml资源文件中可以开展逻辑判断吗

发布时间: 2013-02-24 17:58:56 作者: rapoo

App.xaml资源文件中可以进行逻辑判断吗
DataTemplate怎么根据绑定属性的值不同 触发事件呢
比如下面这个 我如果想"Result"值 是false的时候把TextBlock的背景色改成红色 可以办到吗,求大能指导哈


<DataTemplate DataType="{x:Type c:Test}">
<StackPanel VerticalAlignment="Center" Orientation="Vertical" Height="200">
<StackPanel VerticalAlignment="Center" Orientation="Horizontal">
<TextBlock Text="{Binding Result}"></TextBlock>
</StackPanel>
</StackPanel>

</DataTemplate>

[解决办法]
public class StringColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string item = value.ToString();
SolidColorBrush brush = new SolidColorBrush();
if (item == "False")
{
brush.Color = Colors.Red;
}
else if (item == "True")
{
brush.Color = Colors.Blue;
}
return brush;
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}



<DataTemplate DataType="{x:Type c:Test}">
<StackPanel VerticalAlignment="Center" Orientation="Vertical" Height="200">
<StackPanel VerticalAlignment="Center" Orientation="Horizontal">


<TextBlock Text="{Binding Result}" Foreground="{Binding Result,Converter={StaticResource StringColorConverter}}}"></TextBlock> </StackPanel>
</StackPanel>

</DataTemplate>

读书人网 >CAD教程

热点推荐