繁体   English   中英

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

[英]Binding(Converter) in Code Behind

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

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

我不知道什么是绑定源,或者饼图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);
    }
}

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

我希望这有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM