代码背后的绑定(转换器)

5 c# wpf xaml datatemplate staticresource

<local:LabelTemp x:Key="labelTemplate"/>
        <DataTemplate x:Key="labelTemp">
            <TextBlock Text="{Binding Converter={StaticResource labelTemplate},Path=Item.Items}"/>
        </DataTemplate>
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我如何将上述Xaml代码写入Code Behind C#.我将此代码用于饼图LabelTemplate.

nto*_*ohl 13

我不知道什么是绑定源,或者饼图LabelTemplate(转换器)是什么样的.我能想出的最好的信息如下:

public class LabelTemplate : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        //...
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        //...
    }
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        LabelTemplate labelTemplateConverter = new LabelTemplate();
        Binding binding = new Binding("Item.Items");
        binding.Converter = labelTemplateConverter;
        txtBlock.SetBinding(TextBlock.TextProperty, binding);
    }
}
Run Code Online (Sandbox Code Playgroud)

并且您的文本块具有名称txtBlock

我希望这有帮助.