代码隐藏中的绑定(转换器)

本文关键字:转换器 绑定 隐藏 代码 | 更新日期: 2023-09-27 18:20:17

<local:LabelTemp x:Key="labelTemplate"/>
        <DataTemplate x:Key="labelTemp">
            <TextBlock Text="{Binding Converter={StaticResource labelTemplate},Path=Item.Items}"/>
        </DataTemplate>

谁能帮我如何将上面的 Xaml 代码编写到 C# 背后的代码中。我将此代码用于饼图标签模板。

代码隐藏中的绑定(转换器)

我不知道

绑定源是什么,或者饼图标签模板(转换器(是什么样子的。我能想出的最好的信息如下:

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);
    }
}

和您的文本块具有名称 txtBlock

我希望这有所帮助。