简体   繁体   English

在某些条件下使用 ComboBox 只读单元格 WPF DataGrid

[英]Make a cell with ComboBox readonly on certain conditions WPF DataGrid

What I have:我拥有的:

I have a DataGridTemplateColumn with a ComboBox :我有一个带有ComboBoxDataGridTemplateColumn

<DataTemplate x:Key="ValuesCellTemplate">
      <TextBlock Text="{Binding Path=SelectedValue, Mode=OneWay}" />
</DataTemplate>
<DataTemplate x:Key="ValuesCellEditingTemplate">
      <ComboBox
            Name="ValuesComboBox"
            DisplayMemberPath="DisplayText"
            ItemsSource="{Binding Path=Value, Mode=OneWay}"
            SelectedValue="{Binding Path=SelectedValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
            SelectedValuePath="DisplayText" />
</DataTemplate>

I am autogenerating the columns, so the AutoGeneratingColumn event looks like this:我正在自动生成列,因此AutoGeneratingColumn事件如下所示:

if (e.PropertyName == "First")
{
    var templateColumn = new DataGridTemplateColumn
    {
        Header = e.PropertyName,
        CellTemplate = (sender as FrameworkElement).FindResource("ValuesCellTemplate") as DataTemplate,
        CellEditingTemplate = (sender as FrameworkElement).FindResource("ValuesCellEditingTemplate") as DataTemplate
    };

    e.Column = templateColumn;
}

When Values (which is an ObservableCollection ) is null I want the cell to be readonly, so it won't be able to enter CellEditing mode.Values (这是一个ObservableCollection )为null我希望单元格是只读的,因此它将无法进入 CellEditing 模式。

There are 2 options for the contents of this column:此列的内容有 2 个选项:

  • a simple integer - SelectedValue一个简单的整数 - SelectedValue
  • ObservableCollection<int> - Values ObservableCollection<int> - Values

When Values has values inside, when double-clicking the cell (or the TextBlock )(so to say - entering CellEditing mode), a ComboBox should appear, otherwise not.Values里面有值时,当双击单元格(或TextBlock )(也就是说 - 进入 CellEditing 模式)时,应该出现一个ComboBox ,否则不会出现。 That's basically it.基本上就是这样。

What I have tried:我尝试过的:

I tried doing it this way:我尝试这样做:

<DataTemplate x:Key="ValuesCellTemplate">
      <TextBlock Text="{Binding Path=SelectedValue, Mode=OneWay}" />
</DataTemplate>
<DataTemplate x:Key="ValuesCellEditingTemplate">
      <ComboBox
            Name="ValuesComboBox"
            DisplayMemberPath="DisplayText"
            IsEnabled="{Binding HasItems, RelativeSource={RelativeSource Self}}"
            ItemsSource="{Binding Path=Value, Mode=OneWay}"
            SelectedValue="{Binding Path=SelectedValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
            SelectedValuePath="DisplayText" />
</DataTemplate>

But when I double-click on it, the TextBox turns into an empty readonly ComboBox (enters CellEditing mode).但是当我双击它时, TextBox变成一个空的只读ComboBox (进入 CellEditing 模式)。 I just want for it to take no action (be readonly)(ONLY when Values is null ).我只希望它不采取任何行动(只读)(仅当Valuesnull )。

What is the proper way to do that?这样做的正确方法是什么?

For the record, I am using MVVM pattern.为了记录,我使用的是 MVVM 模式。

An easy way to prevent the DataGrid from entering the edit mode when you double-click on the TextBox would be to handle the PreviewMouseDown event for the cell:当您双击TextBox时,防止DataGrid进入编辑模式的一种简单方法是处理单元格的PreviewMouseDown事件:

private void OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    DataGridCell cell = (DataGridCell)sender;
    e.Handled = cell.DataContext is YourClass dataObject && !dataObject.Value.Any();
}
...
var templateColumn = new DataGridTemplateColumn
{
    Header = e.PropertyName,
    CellTemplate = (sender as FrameworkElement).FindResource("ValuesCellTemplate") as DataTemplate,
    CellEditingTemplate = (sender as FrameworkElement).FindResource("ValuesCellEditingTemplate") as DataTemplate,
    CellStyle = (sender as FrameworkElement).FindResource("ReadOnlyCellStyle") as Style,
};

XAML : XAML

<Style x:Key="ReadOnlyCellStyle" TargetType="DataGridCell">
    <EventSetter Event="PreviewMouseDown" Handler="OnPreviewMouseDown" />
</Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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