WPF将RadioButtons IsChecked绑定到DataTemplate中TextBlock的Text属性

时间:2016-01-26 00:10:25

标签: wpf binding radio-button

我的应用中有3个RadioButtons:

enter image description here

<StackPanel HorizontalAlignment="Left" Orientation="Horizontal" Margin="91,206,0,24">
       <TextBlock Text="Title Language:" VerticalAlignment="Top" FontSize="20" FontWeight="Bold" HorizontalAlignment="Left" Margin="0,0,15,0" />
       <RadioButton x:Name="Rad_TitleNameRomaji" Content="Romaji" FontSize="20" Margin="0,0,10,0" Checked="TitleSettingsChanged"/>
       <RadioButton x:Name="Rad_TitleNameEnglish" Content="English"  FontSize="20" Margin="0,0,10,0" Checked="TitleSettingsChanged"/>
       <RadioButton x:Name="Rad_TitleNameJapanese" Content="Japanese"  FontSize="20" Checked="TitleSettingsChanged" />
</StackPanel>

我的ListViewItem有一个DataTemplate:

<DataTemplate x:Key="ItemTemplate_ListViewItems" >
        <Grid Width="213" Height="326">
            ...
            <TextBlock Text="{Binding WhatShouldIPutHere}) />
            ...
        </Grid>
</DataTemplate>

ListView&#39; ItemsSourceList<CustClass>

CustClass:

public class CustClass : INotifyPropertyChanged
{
    public string RomajiTitle { get; set; }
    public string EnglishTitle { get; set; }
    public string JapaneseTitle { get; set; }
    ...
    public event PropertyChangedEventHandler PropertyChanged;
}

现在我想要的是当我检查&#34;英语&#34; RadioButtonText内的TextBlock的{​​{1}}将绑定到DataTemplate。对于其他两个人来说也是如此。

我该如何处理?

1 个答案:

答案 0 :(得分:1)

三个DataTriggers应该做的伎俩

<DataTemplate x:Key="ItemTemplate_ListViewItems" >
                <Grid Width="213" Height="326">
                    <TextBlock >
                        <TextBlock.Style>
                            <Style TargetType="TextBlock">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ElementName=Rad_TitleNameRomaji,Path=IsChecked}" Value="True" >
                                        <Setter Property="Text" Value="{Binding RomajiTitle}"></Setter>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding ElementName=Rad_TitleNameEnglish,Path=IsChecked}" Value="True" >
                                        <Setter Property="Text" Value="{Binding EnglishTitle}"></Setter>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding ElementName=Rad_TitleNameJapanese,Path=IsChecked}" Value="True" >
                                        <Setter Property="Text" Value="{Binding JapaneseTitle}"></Setter>
                                  </DataTrigger>
                              </Style.Triggers>
                          </Style>
                      </TextBlock.Style>
                  </TextBlock>
              </Grid>
          </DataTemplate>