绑定datatemplate textblock标签

时间:2014-09-02 15:12:41

标签: c# wpf bind

我有代码:                                                                                                                             
                                      

    <DataTemplate x:Key="SelectedTime">
        <Border BorderBrush="DimGray" BorderThickness="4" Margin="-3,-2,-2,-4" Height="35">
            <Grid Width="61" Height="31" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="LightGray" >
                <Grid.RowDefinitions>
                    <RowDefinition Height="14"/>
                    <RowDefinition Height="15"/>
                </Grid.RowDefinitions>
                <Grid Background="LightBlue" Grid.Row="0"  Margin="0,0,0,0">
                    <TextBlock TextBlock.FontSize="12" HorizontalAlignment="Left" VerticalAlignment="Stretch" TextAlignment="Center" Margin="6,-2,0,0" 
                               Text="{TemplateBinding Tag}"/>
                </Grid>
                <TextBlock Grid.Row="1" Text="{TemplateBinding Content}" HorizontalAlignment="Left" VerticalAlignment="Stretch" TextAlignment="Center" TextBlock.FontSize="10" Height="12" Margin="16,-2,0,0"/>
            </Grid>
        </Border>
    </DataTemplate>

    <Style x:Key="LowerRadioButton"
               TargetType="RadioButton"
               BasedOn="{StaticResource {x:Type ToggleButton}}">
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Background" Value="Transparent"/>
        <Style.Triggers>
            <Trigger Property="IsChecked" Value="True">
                <Setter Property="ContentTemplate" Value="{StaticResource SelectedTime}" />
            </Trigger>
            <Trigger Property="IsChecked" Value="False">
                <Setter Property="ContentTemplate" Value="{StaticResource OrdinaryTime}" />
            </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>


<Grid Height="35" x:Name="testGrid">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="60"/>
        <ColumnDefinition Width="60"/>
        <ColumnDefinition Width="60"/>
        <ColumnDefinition Width="60"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="20"/>
        <RowDefinition Height="15"/>
    </Grid.RowDefinitions>

    <RadioButton Style="{StaticResource LowerRadioButton}" Content="12.10" Tag="1" Grid.Column="0" Grid.RowSpan="2" />
    <RadioButton Style="{StaticResource LowerRadioButton}" Content="13.10" Tag="2" Grid.Column="1"  Grid.RowSpan="2" />
    <RadioButton Style="{StaticResource LowerRadioButton}" Content="14.10" Tag="3" Grid.Column="2"  Grid.RowSpan="2" IsChecked="True" />

</Grid >

我需要绑定到DataTemplate中的标记。尝试通过construnction“{Binding Path = Tag,RelativeSource = {RelativeSource TemplatedParent}}”,但它没有绑定,显示空文本。我做错了什么?

1 个答案:

答案 0 :(得分:1)

您应该知道TemplateBinding只能用于ControlTemplate,而不能DataTemplate。所以你可以尝试这样的解决方法:

<TextBlock TextBlock.FontSize="12" HorizontalAlignment="Left" 
           VerticalAlignment="Stretch" TextAlignment="Center" Margin="6,-2,0,0" 
           Text="{Binding Tag, 
                  RelativeSource={RelativeSource AncestorType=RadioButton}}"/>