views:

30

answers:

2

I have a TextBlock in resource data template:

<DataTemplate x:Key="MyDataTemplate" ItemsSource="{Binding MySource}">
    <TextBlock x:Name="MyText" Text="{Binding ???}" />
</DataTemplate>

that I want to bind with Content of checked radio button:

<RadioButton GroupName="MyGroup" Content="Code" />
<RadioButton GroupName="MyGroup" Content="Description" />

if Code radio button is selected, then I want the text become Text={Binding Code}.

Please help, thank you.

+1  A: 

Assuming this is backed by a viewmodel, you would set up your viewmodel thusly:

bool isCodeChecked;
public bool IsCodeChecked 
{ 
    get { return isCodeChecked; }
    set
    {
        if(value == isCodeChecked) return;
        isCodeChecked = value;
        // raise notification that ***MyText*** property has changed (INotifyPropertyChanged interface)
    }

public string MyText 
{
    get { return IsCodeChecked ? "Code" : "Description"; }
}

Then set up your XAML:

<RadioButton GroupName="MyGroup" Content="Code" IsChecked="{Binding IsCodeChecked, Mode=OneWayToSource}" />

<DataTemplate x:Key="MyDataTemplate" ItemsSource="{Binding MySource}">
     <TextBlock x:Name="MyText" Text="{Binding MyText}" />
</DataTemplate>

The binding on the CheckBox will cause the boolean property on the viewmodel to be updated, which in turn will notify the textbox to update its bound value.

Jay
It didn't work, Jay. MyText had been changed and notified, but my TreeView didn't show binded text (blank).
Jeaffrey Gilbert
What `TreeView`?
Jay
my RadTreeView is using that MyDataTemplate as its ItemTemplate.
Jeaffrey Gilbert
@Jeaffrey Can you show the viewmodel code that you're using?
Jay
A: 

I created 2 DataTamplate in resources, and I switch my TreeView's ItemTemplate from code behind

if (ViewByCodeRadioButton.IsChecked == true)
    MyTreeView.ItemTemplate = Resources["MyDataTemplateCode"] as DataTemplate;
else
    MyTreeView.ItemTemplate = Resources["MyDataTemplateDesc"] as DataTemplate;
Jeaffrey Gilbert