带有复选框的DataGrid单元格MultiSelect ComboBox

时间:2018-12-04 23:12:44

标签: c# wpf

我有一个显示数据网格的wpf MVVM应用程序。我有几列是DataGridTextColumns,我希望另一列是MultiSelect Combobox,其中每行上都显示一个复选框和文本块。 datagrid的dataSource与我正在努力的multiselect组合框的dataSource不同。我的问题是,当它在datagrid单元格中时,我无法在multiselect组合框中看到数据(列表)。当我将代码移到datagrid之外时,我可以看到数据。这是我的下面的代码

ViewModel:

  public class TripInfo : ViewModelBase
{
    public TripInfo(bool isVisited, string cityName)
    {
        IsVisited = isVisited;
        CityName = cityName;
    }

    public Boolean IsVisited { get; set; }

    public String CityName { get; set; }

}

public class DataGridViewModel : ViewModelBase
{
    ObservableCollection<RecordInfo> infos;
    List<TripInfo> tripinfos;
    ICommand _command;


    public ObservableCollection<RecordInfo> PersonsInfo
    {
        get
        {
            return infos;
        }
        set
        {
            infos = value;
            OnPropertyChanged("PersonsInfo");
        }
    }

    public List<TripInfo> TripsInfo
    {
        get
        {
            return tripinfos;
        }
        set
        {
            tripinfos = value;
            OnPropertyChanged("TripsInfo");
        }
    }


    public DataGridViewModel()
    {
        PersonsInfo = new ObservableCollection<RecordInfo>();
        TripsInfo = new List<TripInfo>();

        TripsInfo.Add(new TripInfo(false, "Miami"));
        TripsInfo.Add(new TripInfo(true, "Boston"));
        TripsInfo.Add(new TripInfo(true, "Los Angeles"));
        TripsInfo.Add(new TripInfo(true, "Houston"));
        TripsInfo.Add(new TripInfo(false, "Dallas"));
        TripsInfo.Add(new TripInfo(false, "Atlantic City"));
        TripsInfo.Add(new TripInfo(true, "Chicago"));

        GetPersonInfoData();
    }

    private void GetPersonInfoData()
    {
        PersonsInfo.Add(new RecordInfo
        {
            Name = "AA",
            Age = 24,
            DateOfBirth = new DateTime(1987, 4, 29),
            Address = "XXX XXX XXXX"
        });
        PersonsInfo.Add(new RecordInfo
        {
            Name = "BB",
            Age = 23,
            DateOfBirth = new DateTime(1988, 3, 4),
            Address = "XXX XXXXX XXX"
        });
        PersonsInfo.Add(new RecordInfo
        {
            Name = "CC",
            Age = 26,
            DateOfBirth = new DateTime(1985, 10, 2),
            Address = "XXX XXX X"
        });

View.Xaml

   <Window.Resources>
    <Style x:Key="NameCellStyle" TargetType="DataGridCell">
        <Style.Setters>
            <Setter Property="TextBlock.TextAlignment" Value="Center"/>
            <Setter Property="Background" Value="Aqua"/>
        </Style.Setters>
    </Style>
</Window.Resources>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="35"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <ComboBox Grid.Row="0" Name="cmb" Margin="5" Height="20"  ItemsSource="{Binding TripsInfo}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <CheckBox Margin="5" IsChecked="{Binding IsVisited}"/>
                     <TextBlock Margin="5" Text="{Binding CityName}"/>
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

    <DataGrid Grid.Row="1" Name="DGVPersonInfo" ItemsSource="{Binding PersonsInfo}" AutoGenerateColumns="False" CanUserAddRows="False">
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid>
                            <Button Content="Remove..." Margin="3" 
                                    Command="{Binding Path=DataContext.RemoveCommand, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                                    CommandParameter="{Binding}"/>
                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}">
                <DataGridTextColumn.CellStyle>
                    <Style>
                        <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center" />
                        <Setter Property="FrameworkElement.VerticalAlignment" Value="Center" />
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Header="Age" Binding="{Binding Age}" CellStyle="{StaticResource NameCellStyle}"/>
            <DataGridTemplateColumn Header="Date Of Birth">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding DateOfBirth}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <DatePicker SelectedDate="{Binding DateOfBirth}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn Header="Address" Binding="{Binding Address}" />
            <DataGridTemplateColumn Header="Template">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding TripsInfo}">
                            <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                        <CheckBox IsChecked="{Binding IsVisited}" Width="20" />
                                    <TextBlock Text="{Binding CityName}" Width="100" />
                                </StackPanel>
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                        </ComboBox>

                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

        </DataGrid.Columns>
    </DataGrid>
</Grid>

1 个答案:

答案 0 :(得分:0)

TemplateitemSource内部的任何属性都不能直接由DataGrid的DataContext或任何其他其他控件或窗口访问。

我们需要明确提供数据路径。

根据您的Xaml代码,我不确定您在何处绑定了视图模型,但是根据您的“删除按钮”命令,下面是必须绑定下拉<ComboBox ItemsSource="{Binding Path=DataContext.TripsInfo, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"> 的方式。

Friend: Hey

Bot: I'm afk