如何将 Datagridcombobox 单元格更改为文本框?

发布于 2024-10-15 20:33:25 字数 2612 浏览 1 评论 0原文

我需要将第一行的第一个单元格更改为空白文本框。 现在我将该列作为 datagridviewcombobox 列?

                    <DataGridComboBoxColumn Header="And/Or" Width="60" ItemsSource="{Binding Source={StaticResource PredicateCombinationOperatorsEnumValues}}" SelectedItemBinding="{Binding PredicateCombinationOperator, Mode=TwoWay}" />
                    <DataGridComboBoxColumn Header="Field" ItemsSource="{Binding Source={StaticResource FieldTypeEnumValues}}" SelectedItemBinding="{Binding FieldType}"/>
                    <DataGridComboBoxColumn Header="Operator" MinWidth="70" ItemsSource="{Binding Source={StaticResource OperatorsEnumValues}}" SelectedItemBinding="{Binding Operator}"/>
                    <DataGridTextColumn Header="Value" MinWidth="100" Width="*"  Binding="{Binding Expression}"/>
                </DataGrid.Columns>
                <DataGrid.InputBindings>
                    <KeyBinding Command="{Binding Source={StaticResource DeleteContextMenuCommand}}" Key="Delete"/>
                </DataGrid.InputBindings>
                <DataGrid.ContextMenu>
                    <ContextMenu ItemsSource="{Binding MenuOptions}">
                        <ContextMenu.ItemContainerStyle>
                            <Style TargetType="{x:Type MenuItem}">
                                <Setter Property="Header" Value="{Binding Name}" />
                                <Setter Property="Command" Value="{Binding}" />
                                <Setter Property="Visibility" Value="{Binding IsVisible, Converter={StaticResource BoolToVisibilityConverter}}" />
                                <Setter Property="AutomationProperties.AutomationId" Value="{Binding Name}" />
                                <Setter Property="AutomationProperties.Name" Value="{Binding Name}" />
                            </Style>
                        </ContextMenu.ItemContainerStyle>
                    </ContextMenu>
                </DataGrid.ContextMenu>
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="ContextMenuOpening">
                        <trigger:ContextMenuOpeningTriggerAction/>
                    </i:EventTrigger>
                    <i:EventTrigger EventName="SelectionChanged">
                        <trigger:SelectionChangeTriggerAction/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </DataGrid>

I need to change the 1st cell of 1st row to a blank text box.
right now I have the column as datagridviewcombobox column?

                    <DataGridComboBoxColumn Header="And/Or" Width="60" ItemsSource="{Binding Source={StaticResource PredicateCombinationOperatorsEnumValues}}" SelectedItemBinding="{Binding PredicateCombinationOperator, Mode=TwoWay}" />
                    <DataGridComboBoxColumn Header="Field" ItemsSource="{Binding Source={StaticResource FieldTypeEnumValues}}" SelectedItemBinding="{Binding FieldType}"/>
                    <DataGridComboBoxColumn Header="Operator" MinWidth="70" ItemsSource="{Binding Source={StaticResource OperatorsEnumValues}}" SelectedItemBinding="{Binding Operator}"/>
                    <DataGridTextColumn Header="Value" MinWidth="100" Width="*"  Binding="{Binding Expression}"/>
                </DataGrid.Columns>
                <DataGrid.InputBindings>
                    <KeyBinding Command="{Binding Source={StaticResource DeleteContextMenuCommand}}" Key="Delete"/>
                </DataGrid.InputBindings>
                <DataGrid.ContextMenu>
                    <ContextMenu ItemsSource="{Binding MenuOptions}">
                        <ContextMenu.ItemContainerStyle>
                            <Style TargetType="{x:Type MenuItem}">
                                <Setter Property="Header" Value="{Binding Name}" />
                                <Setter Property="Command" Value="{Binding}" />
                                <Setter Property="Visibility" Value="{Binding IsVisible, Converter={StaticResource BoolToVisibilityConverter}}" />
                                <Setter Property="AutomationProperties.AutomationId" Value="{Binding Name}" />
                                <Setter Property="AutomationProperties.Name" Value="{Binding Name}" />
                            </Style>
                        </ContextMenu.ItemContainerStyle>
                    </ContextMenu>
                </DataGrid.ContextMenu>
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="ContextMenuOpening">
                        <trigger:ContextMenuOpeningTriggerAction/>
                    </i:EventTrigger>
                    <i:EventTrigger EventName="SelectionChanged">
                        <trigger:SelectionChangeTriggerAction/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </DataGrid>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉月流沐 2024-10-22 20:33:25

您可以指定条件数据模板并根据您的条件选择模板。首先,我们必须从 DataTemplateSelector 继承类并定义 DataTemplate 类型的属性。定义与您想要的数据模板一样多的属性。然后重写 SelectTemplate 方法以返回您想要的数据模板。检查下面的示例代码

  <Window.Resources>
    <local:Animals x:Key="animals"/>
    <DataTemplate x:Key="TextTemplate">
        <TextBox Margin="2" Width="60" />
    </DataTemplate>
    <DataTemplate x:Key="ComboTemplate" >
        <ComboBox Width="60" />
    </DataTemplate>
</Window.Resources>
<Grid>
    <Controls:DataGrid>
        <Controls:DataGrid.Columns>
            <Controls:DataGridTemplateColumn Header="And/Or" Width="60">
                <Controls:DataGridTemplateColumn.CellTemplateSelector>
                    <local:CustomTemplateSelector
        TextTemplate="{StaticResource TextTemplate}"
        ComboTemplate="{StaticResource ComboTemplate}"/>
                </Controls:DataGridTemplateColumn.CellTemplateSelector>
            </Controls:DataGridTemplateColumn>

            <Controls:DataGridComboBoxColumn Header="Field"/>
            <Controls:DataGridComboBoxColumn Header="Operator" MinWidth="70" />
            <Controls:DataGridTextColumn Header="Value" MinWidth="100" Width="*"/>
        </Controls:DataGrid.Columns>

    </Controls:DataGrid>

</Grid>



    public class CustomTemplateSelector : DataTemplateSelector
{
  public DataTemplate TextTemplate
  { get; set; }

  public DataTemplate ComboTemplate
  { get; set; }

  public override DataTemplate SelectTemplate(object item, DependencyObject container)
  {
     MyObject obj = item as MyObject;

    if (obj != null)
    {
            // custom logic to select appropriate data template and return
    }
    else
      return base.SelectTemplate(item, container);
  }


  }
}

以了解更多信息,请在此处查看

http://zamjad.wordpress.com/2010/08/23/apply-conditional-data-template-in-data -网格/

You could specify conditional data template and select the template according to your condition. First we have to inherit class from DataTemplateSelector and define properties of DataTemplate type. Define as much properties as data template you want.Then override the SelectTemplate method to return the datatemplate you want.Check the below sample code

  <Window.Resources>
    <local:Animals x:Key="animals"/>
    <DataTemplate x:Key="TextTemplate">
        <TextBox Margin="2" Width="60" />
    </DataTemplate>
    <DataTemplate x:Key="ComboTemplate" >
        <ComboBox Width="60" />
    </DataTemplate>
</Window.Resources>
<Grid>
    <Controls:DataGrid>
        <Controls:DataGrid.Columns>
            <Controls:DataGridTemplateColumn Header="And/Or" Width="60">
                <Controls:DataGridTemplateColumn.CellTemplateSelector>
                    <local:CustomTemplateSelector
        TextTemplate="{StaticResource TextTemplate}"
        ComboTemplate="{StaticResource ComboTemplate}"/>
                </Controls:DataGridTemplateColumn.CellTemplateSelector>
            </Controls:DataGridTemplateColumn>

            <Controls:DataGridComboBoxColumn Header="Field"/>
            <Controls:DataGridComboBoxColumn Header="Operator" MinWidth="70" />
            <Controls:DataGridTextColumn Header="Value" MinWidth="100" Width="*"/>
        </Controls:DataGrid.Columns>

    </Controls:DataGrid>

</Grid>



    public class CustomTemplateSelector : DataTemplateSelector
{
  public DataTemplate TextTemplate
  { get; set; }

  public DataTemplate ComboTemplate
  { get; set; }

  public override DataTemplate SelectTemplate(object item, DependencyObject container)
  {
     MyObject obj = item as MyObject;

    if (obj != null)
    {
            // custom logic to select appropriate data template and return
    }
    else
      return base.SelectTemplate(item, container);
  }


  }
}

for more check here

http://zamjad.wordpress.com/2010/08/23/apply-conditional-data-template-in-data-grid/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文