简体   繁体   English

尝试更改DataGrid中的单元格颜色时,WPF格式的InvalidOperationException

[英]InvalidOperationException in WPF form when trying to change cell color in DataGrid

I cannot say that I am new to WPF, because it would be too much. 我不能说我是WPF的新手,因为那太多了。 I just was given with WPF app to maintain... 我只是得到了WPF应用程序来维护...

I need to change particular cell color in DataGrid based on a value. 我需要基于一个值更改DataGrid的特定单元格颜色。 I thought it would be easy, found that SO post: Change DataGrid cell colour based on values . 我认为这很容易,因此发现: 根据值更改DataGrid单元格颜色

Pasted where it belongs, which gave me the following: 粘贴它所属的位置,这给了我以下几点:

<DataGrid x:Name="DgDevices" ItemsSource="{Binding}" BorderThickness="2,0,2,2" Cursor="Cross">
    <DataGrid.ContextMenu>
        <ContextMenu >
            <MenuItem Header="Załóż Deblokadę" Click="InsertDBL"  />
            <MenuItem Header="Usuń Deblokadę" Click="RemoveDBL"/>
        </ContextMenu>
    </DataGrid.ContextMenu>
    <DataGridTextColumn Binding="{Binding Name}">
        <DataGridTextColumn.ElementStyle>
            <Style TargetType="{x:Type TextBlock}">
                <Style.Triggers>
                    <Trigger Property="Text" Value="1">
                        <Setter Property="Background" Value="Black"/>
                        <Setter Property="Foreground" Value="White"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </DataGridTextColumn.ElementStyle>
    </DataGridTextColumn>
</DataGrid>

Now, when invoking Show method on this form, it gives me InvalidOperationException . 现在,在此表单上调用Show方法时,它给了我InvalidOperationException I searched for explanation why this happens, but haven't found clear explanation. 我搜索了为什么会发生这种情况的解释,但没有找到明确的解释。

Also, I know that Binding Name is placeholder for my binding (in <DataGridTextColumn Binding="{Binding Name}"> ), so I tired putting just Binding there (inspired by ItemsSource="{Binding}" in DataGrid node), but didn't solve the issue. 另外,我知道Binding Name是我的绑定的占位符(在<DataGridTextColumn Binding="{Binding Name}"> ),所以我厌倦了仅将Binding放在那里(受DataGrid节点中的ItemsSource="{Binding}"启发),但是没有解决问题。

You are now adding DataGridTextColumn right into DataGrid itself, not to its columns list. 现在,您正在将DataGridTextColumn直接添加到DataGrid本身,而不是添加到其列列表中。 Adding items directly and using ItemsSource are mutually exclusive, so InvalidOperationException is thrown (and you didn't intend to add column as item anyway). 直接添加项目和使用ItemsSource是互斥的,因此会引发InvalidOperationException (而且您也无意将列添加为项目)。 Instead, do it like this: 而是这样做:

<DataGrid x:Name="DgDevices"
          ItemsSource="{Binding}"
          BorderThickness="2,0,2,2"
          AutoGenerateColumns="False"
          Cursor="Cross">
    <DataGrid.ContextMenu>
        <ContextMenu >
            <MenuItem Header="Załóż Deblokadę" Click="InsertDBL"  />
            <MenuItem Header="Usuń Deblokadę" Click="RemoveDBL"/>
        </ContextMenu>
    </DataGrid.ContextMenu>
    <DataGrid.Columns> <!-- add to columns -->
        <DataGridTextColumn Binding="{Binding Name}">
            <DataGridTextColumn.ElementStyle>
                <Style TargetType="{x:Type TextBlock}">
                    <Style.Triggers>
                        <Trigger Property="Text"
                                 Value="1">
                            <Setter Property="Background"
                                    Value="Black" />
                            <Setter Property="Foreground"
                                    Value="White" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

Also, because you need to set AutoGenerateColumns to False , because otherwise DataGrid will automatically generate columns from your data source, in addition to columns you define manually, and you rarely need that. 另外,由于您需要将AutoGenerateColumns设置为False ,否则, DataGrid除了手动定义的列之外,还会从数据源自动生成列,并且您很少需要它。

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

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