如何在XAML绑定中使用XML键和值 [英] How to use xml key and values in xaml binding
本文介绍了如何在XAML绑定中使用XML键和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<?xml version="1.0" encoding="utf-8" ?>
<LanguageDictionary>
<item key="ButtonBackground" value="Red"/>
</LanguageDictionary>
<ResourceDictionary>
<XmlDataProvider x:Key="brushes" Source="LayoutConfiguration.xml" XPath="/LanguageDictionary/item"/>
<conv:StringToColorConverter x:Key="StringToColorConverter"/>
</ResourceDictionary>
<SolidColorBrush x:Key="Pink01Brush" Color="{Binding Source={StaticResource brushes},Converter={StaticResource StringToColorConverter}, XPath=ButtonBackground}" />
我已经像上面那样编码了.但是相应的笔刷颜色值不会从xml更新.
I have coded like above. But corresponding brush color value is not updated from xml. Is it possible to change the brush style using xml in xaml?
推荐答案
Jayapradha,
Hi Jayapradha,
>>可以在xaml中使用xml更改画笔样式吗?
请按照以下步骤操作并尝试
Please follow the below steps and have a try.
ResourceDictionary XAML:
ResourceDictionary XAML:
<!--<XmlDataProvider x:Key="brushes" Source="testxml.xml" XPath="/LanguageDictionary/item"/>-->
<XmlDataProvider x:Key="testxmlbrushes" XPath="LanguageDictionary/item">
<x:XData>
<LanguageDictionary xmlns="">
<item key="ButtonBackground" value="Red"/>
</LanguageDictionary>
</x:XData>
</XmlDataProvider>
<SolidColorBrush x:Key="Pink01Brush" Color="{Binding Source={StaticResource testxmlbrushes},XPath=@value,Converter={StaticResource StringToColorConverter}}" />
窗口:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="C:\Users\Desktop\Wpf_Example\YourSolidColorBrushdic.xaml "></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Rectangle Width="200" Height="100" Stroke="Black" StrokeThickness="4">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Color, Source={StaticResource Pink01Brush}}" />
</Rectangle.Fill>
</Rectangle>
</Grid>
App.xaml:
<Application.Resources>
<local:StringToColorConverter x:Key="StringToColorConverter"/>
</Application.Resources>
StringToColorConverter:
StringToColorConverter:
public class StringToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((string)value == "Green")
{
return Colors.Green;
}
else if ((string)value == "Red")
{
return Colors.Red;
}
else
{
return Colors.Black;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
最好的问候,
Best Regards,
吕汉楠
这篇关于如何在XAML绑定中使用XML键和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文