Binding(Converter) in Code Behind
Asked Answered
W

1

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

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

Ween answered 2/11, 2015 at 15:58 Comment(0)
S
25

I don't know what is the binding source, or how the Pie Chart LabelTemplate (converter) looks 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);
    }
}

and Your textblock have the Name txtBlock

I hope this helps.

Shanitashank answered 2/11, 2015 at 16:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.