我学习WPF,这很有趣但很混乱。我目前正在尝试从DataTemplate
获取值。无法识别简单Binding = {Binding Value}
。如何获得元素值DataTemplate
?以下代码仅显示值:
<ScrollViewer>
<Grid x:Name="mainGrid" Margin="10,10,10,10">
<DataGrid x:Name="appSettingsData" AutoGenerateColumns="False" Grid.ColumnSpan="6" Grid.Row="7" CanUserAddRows="False" >
<DataGrid.Columns>
<DataGridTemplateColumn Header="Value" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellStyle>
<Style TargetType="ContentControl">
<Style.Triggers>
<DataTrigger Binding="{Binding Type}" Value="textBox">
<Setter Property="ContentTemplate">
<Setter.Value>
<!-- This is place from where I'm trying to take values -->
<DataTemplate>
<TextBox Text="{Binding Value}" />
</DataTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</ScrollViewer>
答案 0 :(得分:1)
为什么让事情变得复杂? 您的控件应位于第一个DataTemplate中:
<ScrollViewer>
<Grid x:Name="mainGrid" Margin="10,10,10,10">
<DataGrid x:Name="appSettingsData" AutoGenerateColumns="False" Grid.ColumnSpan="6" Grid.Row="7" CanUserAddRows="False" >
<DataGrid.Columns>
<DataGridTemplateColumn Header="Value" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value}">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Visibility" Value="Hidden"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Type}" Value="textBox">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</ScrollViewer>