【问题标题】:DataGridTemplateColumn.CellEditingTemplate converting a TextBox to Combobox inconsistentDataGridTemplateColumn.CellEditingTemplate 将 TextBox 转换为 Combobox 不一致
【发布时间】:2020-08-05 20:46:39
【问题描述】:

我有以下 XAML 代码:

<DataGrid ItemsSource="{Binding TempParameters}" AutoGenerateColumns="False" CanUserAddRows="False" Height="100" Name="TempParameterGrid" CanUserSortColumns="False" Width="{Binding Source={StaticResource theWidth}}" MouseDown="SetSelectedGrid">
     <DataGrid.Columns>
         <DataGridTextColumn Width="*" Header="ID"  IsReadOnly="True" Binding="{Binding ID}"/>
         <DataGridTemplateColumn Header="Parameter" IsReadOnly="False" >
             <DataGridTemplateColumn.CellEditingTemplate>
                 <DataTemplate>
                     <ComboBox SelectedValue="{Binding name, Mode=TwoWay}" ItemsSource ="{Binding ParameterNames, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
                 </DataTemplate>
             </DataGridTemplateColumn.CellEditingTemplate>
             <DataGridTemplateColumn.CellTemplate>
                 <DataTemplate>
                     <TextBox Text ="{Binding name, Mode=TwoWay}" />
                 </DataTemplate>
             </DataGridTemplateColumn.CellTemplate> 
         </DataGridTemplateColumn>
         <DataGridTextColumn Width="*" Header="Start"      IsReadOnly="False" Binding="{Binding start}"/>
         <DataGridTextColumn Width="*" Header="End"        IsReadOnly="False" Binding="{Binding end}"/>
         <DataGridTextColumn Width="*" Header="Samples"    IsReadOnly="False" Binding="{Binding samples}"/>
     </DataGrid.Columns>
 </DataGrid>

DataGrid 绑定到集合属性TempParameters。这是以下对象的集合。

    public class TuneParameterRecord {
        public int ID { get; set; }
        public string name { get; set; }
        public double start { get; set; }
        public double end { get; set; }
        public int samples { get; set; }
    }

所有列都是可编辑的。但是我需要 name 列有一个预定义名称的列表。因此,我正在考虑提供一个带有可接受值列表的 ComboBox(虽然很多人会 指出,这个列表不能是枚举。我需要这是一个字符串列表)。这份名单是 在窗口数据上下文中提供。

我可以直接引用它,在 CellEditingTemplate 中提供相对源。我的问题是,当我尝试编辑相关列中的单元格时,我得到一个可以编辑文本框的光标 得到一个组合框。

为了得到组合框。我必须在附图中用红色表示的区域多次单击。第一张图片显示了可以使用光标编辑的单元格,而第二张图片显示了可以使用光标编辑的单元格 在我在指示的红色区域中单击多次后,图像具有组合框(虽然被屏蔽)。

【问题讨论】:

  • 正如许多人指出的那样,枚举可以很容易地转换为字符串列表。见stackoverflow.com/questions/14971631
  • This 是您绑定它的方式。
  • @RobertHarvey。感谢您指出这一点。当我研究这个问题时,我看到了关于绑定枚举的问题,这就是我提到它的原因。在这个阶段使用枚举会导致抽象链的一些重构,此时我没有绑定到正确数据的问题,但存在与定义的行为不同的问题。

标签: c# wpf xaml datagridtemplatecolumn


【解决方案1】:

所以我找到了问题所在。我已经转换了

<DataGridTemplateColumn.CellTemplate>
     <DataTemplate>
         <TextBox Text ="{Binding name, Mode=TwoWay}" />
     </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

<DataGridTemplateColumn.CellTemplate>
     <DataTemplate>
         <TextBlock Text ="{Binding name, Mode=TwoWay}" />
     </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

这默认为所需的行为。我感觉我的行为不一致,因为TextBox 可以编辑,而Textblock 不能。因此一个 单击会选择TextBox,但不会恢复到单元格编辑模板。

【讨论】:

  • 您也可以尝试在文本框上设置IsHitTestVisible="false"
【解决方案2】:

我的 XAML 与您在我的代码中发布的基本相同。但是,我的 XAML 还包括以下内容作为组合框的一部分:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <helpers:TakeFocusAction />
    </i:EventTrigger>
</i:Interaction.Triggers>

它的目的是在CellEditingTemplate 启动时将焦点放在组合框中。

TakeFocus 操作如下所示:

using System.Windows;
using System.Windows.Interactivity;

namespace Helpers
{
    public class TakeFocusAction : TriggerAction<UIElement>
    {
        protected override void Invoke(object parameter)
        {
            AssociatedObject.Focus();
        }
    }
}

您的 Xaml 中需要以下命名空间:

xmlns:helpers="clr-namespace:Helpers"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

如果您还没有,则需要Microsoft.Xaml.Behaviors.Wpf Nuget package,它提供了上面的交互命名空间。

【讨论】:

  • 尝试这样做,我得到Severity Code Description Project File Line Suppression State Error XDG0008 The name "Interaction" does not exist in the namespace "http://schemas.microsoft.com/expression/2010/interactivity".
  • 安装this Nuget 包。
猜你喜欢
  • 1970-01-01
  • 2013-04-24
  • 2018-05-24
  • 2016-09-08
  • 1970-01-01
  • 2016-04-21
  • 1970-01-01
  • 2019-06-16
  • 1970-01-01
相关资源
最近更新 更多