2010-07-21 64 views
0

的內容我在資源數據模板的TextBlock:綁定的TextBlock中的DataTemplate(資源),以檢查單選按鈕

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

,我想與檢查單選按鈕的Content綁定:

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

如果選擇代碼單選按鈕,那麼我希望文本變成Text={Binding Code}

請幫忙,謝謝。

回答

0

我創建2 DataTamplate在資源,將我的TreeView的ItemTemplate從後面的代碼

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

假設這是由視圖模型的支持,你將建立您的視圖模型正是如此:

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

然後設置您的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> 

CheckBox的結合將導致布爾屬性在要更新的視圖模型上,這又將通知文本框更新其綁定值。

+0

它沒有工作,周杰倫。 MyText已被更改並通知,但我的TreeView沒有顯示綁定文本(空白)。 – 2010-07-22 03:23:32

+0

什麼'TreeView'? – Jay 2010-07-22 04:04:54

+0

我的RadTreeView使用MyDataTemplate作爲它的ItemTemplate。 – 2010-07-29 09:08:24