简体   繁体   中英

WPF binding inside DataTemplate

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM