gpt4 book ai didi

c# - 绑定(bind)数据模板文本 block 标签

转载 作者:太空宇宙 更新时间:2023-11-03 13:17:23 26 4
gpt4 key购买 nike

我有代码:

    <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 >

我需要绑定(bind)到我的 DataTemplate 中的标记。尝试通过构造“{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}”,但未绑定(bind),显示空文本。我做错了什么?

最佳答案

你应该知道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}}"/>

关于c# - 绑定(bind)数据模板文本 block 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25626762/

26 4 0