后台代码中的绑定(转换器) [英] Binding(Converter) in Code Behind

查看:176
本文介绍了后台代码中的绑定(转换器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

任何人都可以帮助我如何将上述Xaml代码写入C#背后的代码中. 我正在使用此代码到饼图LabelTemplate中.

Can anyone help me how to write the above Xaml code into Code Behind C#. Im using this code into Pie Chart LabelTemplate.

推荐答案

我不是绑定源,也不是饼图标签模板(转换器)的外观.我能提供的最好的信息是:

I don't what is the binding source, or how the Pie Chart LabelTemplate (converter) look like. The best I can come up with that much information is the following:

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

and Your textblock have the Name txtBlock

我希望这会有所帮助.

这篇关于后台代码中的绑定(转换器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆