【问题标题】:Changes not visible in the UI after updating data in the DataGrid更新 DataGrid 中的数据后,更改在 UI 中不可见
【发布时间】:2021-10-22 07:47:11
【问题描述】:

当我通过DataContext修改值为CheckBox设置数据时,为什么向下滚动或向上滚动时返回原始值?有什么办法可以恢复吗?

当我向下或向上滚动时,我希望它不会将复选框恢复为原始值。


XAML 代码

<DataGrid x:Name="list_account_instagram"
          Background="#DDDDDD"
          CanUserResizeColumns="False"
          CanUserSortColumns="False"
          CanUserAddRows="False">
    <DataGrid.Columns>
        <materialDesign:DataGridTextColumn Width="0.4*"
                                           Binding="{Binding STT}"
                                           Header="#" />
        <DataGridCheckBoxColumn Width="0.35*" Binding="{Binding CHECK, Mode=OneTime}">
            <DataGridCheckBoxColumn.Header>
                <Border Background="Transparent">
                    <CheckBox Name="checkallins" Click="Checkallins_Click"/>
                </Border>
            </DataGridCheckBoxColumn.Header>
        </DataGridCheckBoxColumn>

        <materialDesign:DataGridTextColumn Binding="{Binding UID}" Width="*"
                                           Header="UID" />
        <materialDesign:DataGridTextColumn Binding="{Binding PASSWORD}" Width="*"
                                           Header="Password"/>
        <materialDesign:DataGridTextColumn Binding="{Binding COOKIE}" Width="*"
                                           Header="Cookie"/>
        <materialDesign:DataGridTextColumn Binding="{Binding TEN}" Width="*"
                                           Header="Tên"/>
        <materialDesign:DataGridTextColumn Binding="{Binding AVATAR}" Width="*"
                                           Header="Avatar"/>
        <materialDesign:DataGridTextColumn Binding="{Binding TT}" Width="*"
                                           Header="Tình Trạng"/>
        <materialDesign:DataGridTextColumn Binding="{Binding MESSAGE}" Width="*"
                                           Header="Trạng Thái"/>
    </DataGrid.Columns>
</DataGrid>

C#

private void Checkallins_Click(object sender, RoutedEventArgs e)
{
    if (checkallins.IsChecked.Value)
    {
        if (list_account_instagram.Items.Count > 0)
        {
            for (int i = 0; i < list_account_instagram.Items.Count; i++)
            {
              GetCell(list_account_instagram, i, 1).DataContext = new { CHECK = true };
            }
        }
    }
    else
    {
        if (list_account_instagram.Items.Count > 0)
        {
            for (int i = 0; i < list_account_instagram.Items.Count; i++)
            {
              GetCell(list_account_instagram, i, 1).DataContext = null;
              GetCell(list_account_instagram, i, 1).DataContext = new { CHECK = false };
            }
        }
    }
}

为了更好地描述有问题的行为,有视频:https://files.fm/u/2xxgqrqra#/view/p4jw246e3

【问题讨论】:

  • 您是否正在处理向上/向下滚动?您是否对数据进行了一些更改?
  • @Tatranskymedved 我没有处理向上和向下滚动的功能

标签: c# wpf datagrid


【解决方案1】:

问题与使用 OneTime 绑定有关。这将导致数据仅在初始化期间加载,但在进行任何更改(在 UI 中或从代码中)之后,它不会影响集合本身。

当您在“UI”中看到不同的东西和在“代码隐藏”中看到不同的东西时,这会导致问题。如果您要调试实际值(当按下 DataGrid 的标题时),您会惊讶于这些值的来源。

修复很简单,你必须更新Binding 模式。而不是这个:

Binding="{Binding CHECK, Mode=OneTime}">

你应该使用:

Binding="{Binding CHECK}">
<!-- or this -->
Binding="{Binding CHECK, Mode=TwoWay}">

编辑:从 OP 的 cmets 看来,问题还在于为 DataGrid 中的项目设置 bool 值。您不应该在那里分配新对象,而是应该使用DataGridDataContext。您可以通过以下方式实现:

var isChecked = checkallins.IsChecked.Value;

foreach(var item in list_account_instagram.Items)
{
    // Put instead of `MyClass` actual class you are using (prob. `InstagramAccount`)
    var myItem = item as MyClass;

    if(item != null)
    {
        myItem.CHECK = isChecked;
    }
}

【讨论】:

  • 当我打电话给GetCell(list_account_instagram, i, 1).DataContext = new { CHECK = true }; 时,我收到如下错误,有什么办法可以解决吗? System.InvalidOperationException: 'A TwoWay or OneWayToSource binding cannot work on the read-only property 'CHECK' of type '&lt;&gt;f__AnonymousType11[System.Boolean]'.'`
猜你喜欢
  • 2014-01-23
  • 1970-01-01
  • 2023-03-19
  • 2014-08-17
  • 1970-01-01
  • 2017-04-28
  • 1970-01-01
  • 2021-11-04
  • 1970-01-01
相关资源
最近更新 更多