Reputation: 1158
I'm learing WPF, it's fun but very confusing. I am currently trying to get values from DataTemplate
. Simple Binding = {Binding Value}
is not recognized. How can I get values of elements iside DataTemplate
? Following code only displays Value:
<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>
Upvotes: 0
Views: 262
Reputation: 879
Why are you complicating things? Your controls should be in the first DataTemplate itself:
<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>
Upvotes: 1