DataTemplate中的WPF绑定

时间:2018-03-06 12:05:11

标签: c# .net wpf data-binding

我学习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>

1 个答案:

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