簡體   English   中英

如何在XAML綁定中使用XML鍵和值

[英]How to use xml key and values in xaml binding

<?xml version="1.0" encoding="utf-8" ?>
<LanguageDictionary>
  <item key="ButtonBackground" value="Red"/>
</LanguageDictionary>

XAML

  <ResourceDictionary>
        <XmlDataProvider x:Key="brushes" Source="LayoutConfiguration.xml" XPath="/LanguageDictionary/item"/>
  </ResourceDictionary>

 <SolidColorBrush x:Key="WhiteBrush" Color="{Binding Source={StaticResource brushes}, XPath=ButtonBackground}" />

我有上面的代碼。 但這是行不通的。 是否可以使用其鍵值從xml更改畫筆顏色。

綁定到的值是字符串,而不是顏色。 因此,您必須創建一個轉換器(從IValueConverter繼承)以將字符串轉換為Color:
public class StringToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var color = System.Windows.Media.ColorConverter.ConvertFromString((string)value);
        return color;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value?.ToString();
    }
}
然后,您可以在綁定中使用此轉換器:

注釋后編輯: XPath確實似乎無效。 請在下面的實際工作示例中找到:

<Window x:Class="TestXmlBinding.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:TestXmlBinding"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <XmlDataProvider Source="LayoutConfiguration.xml" XPath="/LanguageDictionary" x:Key="brushes" />
        <SolidColorBrush x:Key="br" Color="{Binding Source={StaticResource brushes}, XPath=item\[@key\=\'ButtonBackground\']/@value}" />
    </Window.Resources>
    <Grid>
        <TextBlock Foreground="{StaticResource br}" Text="Test" />
    </Grid>
</Window>

請注意,您必須在綁定標記擴展中轉義一些XPath字符。

此外,Color似乎具有關聯的TypeConverter,因此您不需要IValueConverter作為Color類型。

是的,類似於Pavel代碼,即使我嘗試了它,也從上面添加的xml文件中提取了顏色。

XML

<?xml version="1.0" encoding="utf-8" ?> 
<LanguageDictionary>
  <item key="ButtonBackground" value="red"/>
</LanguageDictionary>

資源文件

<Application x:Class="WpfApplication1.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:WpfApplication1"
         StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <XmlDataProvider Source="LayoutConfiguration.xml" XPath="/LanguageDictionary" x:Key="brushes" />
            <SolidColorBrush x:Key="xmlColorKey" Color="{Binding Source={StaticResource brushes}, XPath=item\[@key\=\'ButtonBackground\']/@value}" />
        </ResourceDictionary>
    </Application.Resources>
</Application>

XAML

 <Grid>
        <StackPanel Orientation="Vertical">
            <TextBlock  FontWeight="Bold" HorizontalAlignment="Center" FontSize="30" Background="{StaticResource xmlColorKey}" Text="Color Test" />
        </StackPanel>
    </Grid>

構建應用程序后,它將煥然一新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM