如何禁用新行的特定DataGid单元格

时间:2017-03-05 15:58:35

标签: c# wpf xaml datagrid converter

我有一个简单的DataGrid,有两列,如

<DataGrid ItemsSource="{Binding Commands}" CanUserAddRows="True" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Value" Binding="{Binding Value}"/>
        <DataGridTemplateColumn Header="Command">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding ComboItems}" SelectedValue="{Binding SelectedItem}">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Converter={StaticResource ItemConverter}}"/>
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

我的目标是使用新行ComboBox禁用单元格。下图显示了我的DataGrid(绑定到ObservableCollection两个项目),并且要禁用标记的列。

enter image description here

我已经尝试使用转换器来禁用ComboBox

IsEnabled="{Binding Value, Converter={StaticResource DisableConverter}}"

但转换器无法调用,直到我在第一列内输入值。

希望有人可以帮助我!

2 个答案:

答案 0 :(得分:2)

您可以将以下Style应用于ComboBox

<ComboBox ItemsSource="{Binding ComboItems}" SelectedValue="{Binding SelectedItem}">
    <ComboBox.Style>
        <Style TargetType="ComboBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=DataContext,RelativeSource={RelativeSource AncestorType=DataGridRow}}" 
                                                         Value="{x:Static CollectionView.NewItemPlaceholder}">
                    <Setter Property="IsEnabled" Value="False" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ComboBox.Style>
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Converter={StaticResource ItemConverter}}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

答案 1 :(得分:1)

基于此previous answer,您可以在Loaded事件处理程序

中实现此目的
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        var dataGridRow = FindChild(dataGrid, x =>
        {
            var element = x as DataGridRow;
            if (element != null && element.Item == System.Windows.Data.CollectionView.NewItemPlaceholder)
                return true;
            else
                return false;
        }) as DataGridRow;
        var combo = FindChild(dataGridRow, x =>
        {
            return x is ComboBox;
        }) as ComboBox;
        combo.IsEnabled = false;
    }

有了这个助手

    public static DependencyObject FindChild(DependencyObject parent, Func<DependencyObject, bool> predicate)
    {
        if (parent == null) return null;

        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);

            if (predicate(child))
            {
                return child;
            }
            else
            {
                var foundChild = FindChild(child, predicate);
                if (foundChild != null)
                    return foundChild;
            }
        }

        return null;
    }