使用数据绑定时,列表视图未刷新 [英] listview not refreshed when using databinding

查看:37
本文介绍了使用数据绑定时,列表视图未刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型类Book,其中包含一个关键词属性:

I have a model class, Book, which contains a Keywords property:

public class Book : INotifyPropertyChanged
{
    private ObservableCollection<string> _keywords;
    ...
    public ObservableCollection<string> Keywords
    {
        get => _keywords;
        set
        {
            _keywords = value;
            OnPropertyChanged("Keywords");
        }
    }
}

在我的主页中,我有2个组成部分:一个列表视图和一个组合框,其每个条目都是一个复选框:

and in my MainPage I have 2 components : a list View and a combobox whose each entry is a checkBox:

<ComboBox
    x:Name="cbb_Keywords"
    Grid.Column="2"
    Width="300"
    Margin="5,0,0,0"
    HorizontalAlignment="Left"
    ItemsSource="{Binding Source={StaticResource AllBooks}}"
    DataContext="{Binding ElementName=listBoxBooks,Path=SelectedItem,UpdateSourceTrigger=PropertyChanged}">


    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox Width="200" Content="{Binding}" Click="ButtonBase_OnClick">
                    <CheckBox.IsChecked>
                        <MultiBinding Converter="{StaticResource TextInListTrueFalseConverter}" Mode="OneWay">
                            <Binding ElementName="listBoxBooks" Path="SelectedItem.KeywordsForTextbox" Mode="OneWay"></Binding>
                            <Binding RelativeSource="{RelativeSource Self}" Path="Content"></Binding>
                        </MultiBinding>
                    </CheckBox.IsChecked>
                </CheckBox>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

checkBox.IsChecked多重绑定是单向的,当我单击一个复选框时,它将调用此方法:

the checkBox.IsChecked multibinding is oneway, and when I click on a checkbox, it calls this method:

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
    CheckBox cb = (CheckBox)sender;
    var content = (string)cb.Content;
    var keywords = ((Book)listBoxBooks.SelectedItem).Keywords;
    bool clicked = cb.IsChecked.Value;
    if (clicked)
        keywords.Add(content);
    else
        keywords.Remove(content);  
}

它或多或少起作用,但有两个警告:

it works more or less but there are 2 caveats:

  • 有时,我刚刚单击的复选框会显示在组合框的复选框中,这不是预期的,而且很烦人

  • sometimes the checkbox on which I just clicked is displayed in the combobox's checkbox, which is not expected and is annoying

除了组合框,我还有一个其他组件,一个文本框,其中包含列表视图的selectedItem的关键字列表:

I have, in addition of the combobox, an other component, a textbox, which contains the list of the keywords for the listview's selectedItem:

但是当我单击一个复选框来切换它时,包含该列表的列表框不会被刷新...

but when I click on a checkbox to toogle this, the listbox containing the list is not refreshed...

所以我在Book中做了一些我的关键词属性:

so I chenged a little my Keywords property, in Book:

public ObservableCollection<string> Keywords
{
    get => _keywords;
    set
    {
        _keywords = value;
        OnPropertyChanged("Keywords");
        OnPropertyChanged("KeywordsForTextbox");
    }
}

,KeywordsForTextbox属性如下:

and the KeywordsForTextbox property is like this:

public string KeywordsForTextbox
{
    get { return string.Join(",", _keywords); }    
}

最后,为了完整起见,这是我的MainWindow中的textBox组件:

finally, to be complete, here is the textBox component in my MainWindow:

<TextBox x:Name="txb_Keywords"
         Grid.Column="1"
         Width="500"
         Text="{Binding ElementName=listBoxBooks,Path=SelectedItem.KeywordsForTextbox,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" />

为什么该复选框出现在组合框的文本框中?为什么不刷新另一个文本框?

why does the checkbox appears in the combobox's textbox? why isn't refreshed the other textbox?

谢谢.

推荐答案

问题是,在修改关键字"集合时,实际的关键字"属性不会更改.它仍然是相同的集合对象.仅对象的属性(项)会更改.

The problem is that when modifying the Keywords collection the actual Keywords property doesn't change. It's still the same collection object. Only the object's properties (Items) change.

在Book类中,您可以使用方法进行添加和删除,然后从此处通知属性更改.

In your Book class you could use methods to do the adding, and removing, then notify property changed from there.

public void AddKeyword(string name)
{
    Keywords.Add(name);
    OnPropertyChanged("Keywords");
}

public void RemoveKeyword(string name)
{
    Keywords.Remove(name);
    OnPropertyChanged("Keywords");
}

然后像这样更改您的活动.

Then change your event like this.

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
    CheckBox cb = (CheckBox)sender;
    var content = (string)cb.Content;
    var book = ((Book)listBoxBooks.SelectedItem);
    bool clicked = cb.IsChecked.Value;
    if (clicked)
        book.AddKeyword(content);
    else
        book.RemoveKeyword(content);
}

这篇关于使用数据绑定时,列表视图未刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆