如何使DataGrid行中的ComboBox唯一?

时间:2018-12-14 16:25:38

标签: c# wpf xaml combobox

环境以下问题: C#,WPF,XAML

如何实现设置表/数据网格的行数不确定的任意行在任意列中都有组合框(列数也未定义)? (另一行的单元格将填充底层对象/实体的属性。)ComboBox都应具有相同的项目,但所选的项目应(当然)分配给该行的基础对象。因此,我认为每行的ComboBox必须具有唯一的标识符。 所选项目应存储在基础对象/实体的属性中。

顺便说一句: 组合框应填充不属于该行的基础对象/实体的集合(列表)中的项目。

使用WPF / XAML的最佳方法是什么?

更新(2018-12-14):

    <Window x:Class="ConfigTool.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ConfigTool"
        xmlns:lb="clr-namespace:ConfigTool.DataBinding"
        mc:Ignorable="d"
        Title="xxx" Height="650" Width="1200" Closing="CloseWindow">

    <Window.Resources>
        <CollectionViewSource x:Key="TagsCollectionViewSource" CollectionViewType="ListCollectionView"/>
        <CollectionViewSource x:Key="NotificationsCollectionViewSource" CollectionViewType="ListCollectionView"/>
        <CollectionViewSource x:Key="TagNamesCollectionViewSource" CollectionViewType="ListCollectionView"/>

        <lb:StringListConverter x:Key="StringListConverter" />
    </Window.Resources>

    <Grid Margin="10,10,10,10">
        <TabControl>
            <TabItem Header="Tags">
                <ScrollViewer HorizontalScrollBarVisibility="Auto">
                    <DataGrid x:Name="tagsGrid" DataContext="{StaticResource TagsCollectionViewSource}" ItemsSource="{Binding}" 
                              AlternatingRowBackground="LightBlue" AutoGenerateColumns="False" CanUserAddRows="True" IsReadOnly="False" 
                              SelectionMode="Single" BorderBrush="Magenta" BorderThickness="3">
                        <DataGrid.Columns>
                            <DataGridTextColumn x:Name="TagName" Header="Tag name" Binding="{Binding Mode=TwoWay, Path=TagName}"></DataGridTextColumn>
                            <DataGridTextColumn Header="Cycle" Binding="{Binding Mode=TwoWay, Path=Cycle}"></DataGridTextColumn>
                            <DataGridTextColumn Header="Source" Binding="{Binding Mode=TwoWay, Path=Source}"></DataGridTextColumn>
                            <DataGridTemplateColumn x:Name="editTagColumn" Header="Edit">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <WrapPanel>
                                            <Button x:Name="btnTagDelete" Click="BtnTagDelete_Click" CommandParameter="{Binding}" Height="15" Width="15" Margin="2">
                                                <Button.Content>
                                                    <Image Source="Resources/delete.png"></Image>
                                                </Button.Content>
                                            </Button>
                                        </WrapPanel>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                        </DataGrid.Columns>
                    </DataGrid>
                </ScrollViewer>
            </TabItem>
            <TabItem Header="Notifications">
                <ScrollViewer HorizontalScrollBarVisibility="Auto">
                    <DataGrid x:Name="notificationsGrid" DataContext="{StaticResource NotificationsCollectionViewSource}" ItemsSource="{Binding}" 
                              AlternatingRowBackground="LightBlue" AutoGenerateColumns="False" CanUserAddRows="True" IsReadOnly="False" 
                              SelectionMode="Single" BorderBrush="Magenta" BorderThickness="3">
                        <DataGrid.Columns>
                            <!--<DataGridTextColumn Header="Tag name" Binding="{Binding Mode=TwoWay, Path=TagName}"></DataGridTextColumn>-->
                            <DataGridTemplateColumn x:Name="tagNameColumn" Header="Tag name">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <!--<ItemContainerTemplate>
                                        <StackPanel>-->
                                            <!--DataContext="{Binding Source={StaticResource TagNamesCollectionViewSource}}">-->
                                            <!--<ComboBox Width="200" DataContext="{StaticResource TagNamesCollectionViewSource}" ItemsSource="{Binding TagNames, Converter={StaticResource StringListConverter}}">-->
                                        <ComboBox Name="notificationTagName" Width="200" DataContext="{StaticResource TagNamesCollectionViewSource}" ItemsSource="{Binding Source={StaticResource TagNamesCollectionViewSource}, RelativeSource={RelativeSource AncestorType=local:MainWindow}}" 
                                                  SelectionChanged="notificationTagName_SelectionChanged" />
                                            <!--<ComboBox Width="200" DataContext="{StaticResource TagNamesCollectionViewSource}" ItemsSource="{Binding Converter={StaticResource StringListConverter}}" />-->
                                            <!--<ComboBox Width="200" ItemsSource="{Binding ElementName=Window2, Path=DataContext.TagNames, Converter={StaticResource StringListConverter}}" />-->
                                            <!--<ComboBox HorizontalAlignment="Left" Margin="256,260,0,0" VerticalAlignment="Top" Width="120" x:Name="DataList" ItemsSource="{Binding DetailParams, Converter={StaticResource StringListConverter}}"/>-->
                                    </DataTemplate>
                                    <!--</StackPanel>
                                    </ItemContainerTemplate>-->
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                            <DataGridCheckBoxColumn Header="IsActive" Binding="{Binding Mode=TwoWay, Path=IsActive}"></DataGridCheckBoxColumn>
                            <DataGridTextColumn Header="Type" Binding="{Binding Mode=TwoWay, Path=Type}"></DataGridTextColumn>
                            <DataGridTextColumn Header="Limit" Binding="{Binding Mode=TwoWay, Path=Limit}"></DataGridTextColumn>
                            <DataGridTextColumn Header="DetailTemplate" Binding="{Binding Mode=TwoWay, Path=DetailTemplate}"></DataGridTextColumn>
                            <!--<DataGridTextColumn Header="DetailParams" Binding="{Binding Mode=TwoWay, Path=DetailParams, Converter={StaticResource StringListConverter}}"></DataGridTextColumn>-->
                            <DataGridTemplateColumn x:Name="detailsParamColumn" Header="Edit">
                                <DataGridTemplateColumn.CellTemplate>
                                    <ItemContainerTemplate>
                                        <StackPanel> <!--DataContext="{Binding Source={StaticResource TagNamesCollectionViewSource}}">-->
                                            <!--<ComboBox Width="200" DataContext="{StaticResource TagNamesCollectionViewSource}" ItemsSource="{Binding TagNames, Converter={StaticResource StringListConverter}}">-->
                                            <ComboBox Width="200" DataContext="{StaticResource TagNamesCollectionViewSource}" ItemsSource="{Binding}" />
                                            <!--<ComboBox Width="200" DataContext="{StaticResource TagNamesCollectionViewSource}" ItemsSource="{Binding Converter={StaticResource StringListConverter}}" />-->
                                            <!--<ComboBox Width="200" ItemsSource="{Binding ElementName=Window2, Path=DataContext.TagNames, Converter={StaticResource StringListConverter}}" />-->
                                            <!--<ComboBox HorizontalAlignment="Left" Margin="256,260,0,0" VerticalAlignment="Top" Width="120" x:Name="DataList" ItemsSource="{Binding DetailParams, Converter={StaticResource StringListConverter}}"/>-->
                                        </StackPanel>
                                    </ItemContainerTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                            <DataGridTextColumn Header="CauseProbability" Binding="{Binding Mode=TwoWay, Path=CauseProbability, Converter={StaticResource StringListConverter}}"></DataGridTextColumn>
                            <DataGridTemplateColumn x:Name="editNotificationsColumn" Header="Edit">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <WrapPanel>
                                            <Button x:Name="btnNotificationDelete" Click="BtnNotificationDelete_Click" Height="15" Width="15" Margin="2">
                                                <Image Source="Resources/delete.png"></Image>
                                            </Button>
                                        </WrapPanel>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                        </DataGrid.Columns>
                    </DataGrid>
                </ScrollViewer>
            </TabItem>
        </TabControl>
    </Grid>
</Window>

1 个答案:

答案 0 :(得分:0)

之所以发生这种情况,是因为您为每一行中的组合框使用了相同的集合。 组合框的ItemsSource应该是一个集合,并且应该是设置为DataGrid的DataContext的类的成员/属性。

将Combo-Box的SelectedItem属性绑定到您的类的成员。 参见https://www.c-sharpcorner.com/uploadfile/dpatra/combobox-in-datagrid-in-wpf/