WPF - DataGridTemplate与DataTemplate.Triggers

问题描述 投票:0回答:2

我需要在一列中获得不同类型的行,所以我试图将DataGridTemplate与触发器一起使用。我发送Class = {Value = "Something", Type = "comboBox"}comboBox1没有显示。

    <DataGridTemplateColumn Header="Value" Width="*">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Value}" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
        <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Type}" Value="comboBox">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <ComboBox Height="22" Name="comboBox1">
                                    <ComboBoxItem Content="X"/>
                                    <ComboBoxItem Content="Y"/>
                                    <ComboBoxItem Content="Z"/>
                                </ComboBox>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
        </DataGridTemplateColumn.CellEditingTemplate>
    </DataGridTemplateColumn>
c# .net wpf triggers
2个回答
1
投票

组合框模板列不正确,这很好用:

<DataGrid Name="dg" Width="400" Height="300" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name}"/>
            <DataGridTemplateColumn Header="Value" Width="*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Name="textbox1" Text="{Binding Address}">
                                <TextBlock.Style>
                                    <Style TargetType="{x:Type TextBlock}">
                                        <Setter Property="Visibility" Value="Hidden"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Address}" Value="Pune">
                                                <Setter Property="Visibility" Value="Visible"/>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding Address}" Value="Mumbai">
                                                <Setter Property="Visibility" Value="Hidden"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>
                            <ComboBox Height="22" Name="comboBox1">
                                <ComboBoxItem Content="X"/>
                                <ComboBoxItem Content="Y"/>
                                <ComboBoxItem Content="Z"/>
                                <ComboBox.Style>
                                    <Style TargetType="{x:Type ComboBox}">
                                        <Setter Property="Visibility" Value="Hidden"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Address}" Value="Mumbai">
                                                <Setter Property="Visibility" Value="Visible"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </ComboBox.Style>
                            </ComboBox>
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
</DataGrid>

这是有效的,请将触发器绑定值更改为您的类型。我使用了地址值,例如,你使用你的值。


0
投票

DataTemplate移动到Style以某种方式解决了我的问题,但它仍然没有按预期工作。

    <DataGridTemplateColumn.CellStyle>
        <Style TargetType="ContentControl">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Type}" Value="comboBox">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <ComboBox Height="22" Name="comboBox1">
                                    <ComboBoxItem Content="X"/>
                                    <ComboBoxItem Content="Y"/>
                                    <ComboBoxItem Content="Z"/>
                                </ComboBox>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>                                
        </Style>
    </DataGridTemplateColumn.CellStyle>
© www.soinside.com 2019 - 2024. All rights reserved.