Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 数据模板绑定到后台属性_Wpf_Xaml_Data Binding - Fatal编程技术网

Wpf 数据模板绑定到后台属性

Wpf 数据模板绑定到后台属性,wpf,xaml,data-binding,Wpf,Xaml,Data Binding,我有以下xaml <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

我有以下
xaml

<UserControl
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:Octokit="clr-namespace:Octokit;assembly=Octokit" x:Class="IssuesManagment.UI.POC.Controls.GithubIssue" 
         mc:Ignorable="d" 
         d:DesignHeight="147" d:DesignWidth="295" BorderBrush="#FFD84B4B" BorderThickness="1" Width="Auto" Height="Auto" Margin="0,0,0,2">
<UserControl.DataContext>
    <Octokit:Issue/>
</UserControl.DataContext>
<StackPanel>
    <TextBlock>
        <Hyperlink NavigateUri="{Binding HtmlUrl}" RequestNavigate="Hyperlink_RequestNavigate">
            <TextBlock x:Name="issueTitle" TextWrapping="Wrap" Text="{Binding Title}" FontWeight="Bold" FontSize="16" />
        </Hyperlink>
    </TextBlock>
    <TextBlock x:Name="issueBody" TextWrapping="Wrap" Text="{Binding Body}" Margin="2"/>
    <ListBox ItemsSource="{Binding Labels}">
        <ListBox.ItemTemplate>
            <DataTemplate DataType="Octokit:Label">
                <TextBlock Text="{Binding Name}">
                    <TextBlock.Background>
                        <SolidColorBrush Color="{Binding Color}"></SolidColorBrush>
                    </TextBlock.Background>
                </TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</StackPanel>
</UserControl>

任一属性都正确绑定到TextBlock的文本,但不绑定到它的任何其他属性。

类中的颜色是typeof String not Color,因此需要以下转换器:

public class StringColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var color = (Color)typeof(Colors).GetProperty((String)value).GetValue(null, null);

        return color;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return String.Empty;
    }
}
在XAML中,资源和示例

<Window.Resources>
    <local:StringColorConverter x:Key="StringColorConverter"/>
</Window.Resources>

 <TextBlock Width="200" Height="200">
        <TextBlock.Background>
            <SolidColorBrush Color="{Binding Color, Converter={StaticResource StringColorConverter}}"/>
        </TextBlock.Background>
    </TextBlock>


您是否尝试过此处描述的代理技巧:?谢谢。我不得不对这个代码进行一些调整,因为染色没有前导的
。还有-没有内置的从字符串到颜色的转换器?!
<Window.Resources>
    <local:StringColorConverter x:Key="StringColorConverter"/>
</Window.Resources>

 <TextBlock Width="200" Height="200">
        <TextBlock.Background>
            <SolidColorBrush Color="{Binding Color, Converter={StaticResource StringColorConverter}}"/>
        </TextBlock.Background>
    </TextBlock>