CheckBox双向绑定不起作用 [英] CheckBox two way binding doesn't work

查看:741
本文介绍了CheckBox双向绑定不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对ListView中的复选框进行双向绑定。这是我的产品类:

  public class Product 
{
public bool IsSelected {get;组; }
public string Name {get;组; }
}

在ViewModel类中,我有可观察的产品集合:

  private ObservableCollection< Product> _产品列表; 
public ObservableCollection< Product> ProductList
{
get
{
return _productList;
}
set
{
_productList = value;
}
}

public MainViewModel()
{
ProductList = new ObservableCollection< Product>
{
new Product {IsSelected = false,Name =Not selected},
new Product {IsSelected = true,Name =Selected},
new Product {IsSelected = true,Name =Selected}
};
}
}

最后我有GridView ListView绑定我的ProductList :

 < Grid> 
< ListView Height =120Horizo​​ntalAlignment =Left
VerticalAlignment =Top
SelectionMode =Multiple
ItemsSource ={Binding ProductList}>
< ListView.View>
< GridView>
< GridViewColumn Width =40>
< GridViewColumn.CellTemplate>
< DataTemplate>
< CheckBox IsChecked ={Binding Path = IsSelected,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}/>
< / DataTemplate>
< /GridViewColumn.CellTemplate>
< / GridViewColumn>
< GridViewColumn Width =120Header =Product NameDisplayMemberBinding ={Binding Path = Name}/>
< / GridView>
< /ListView.View>
< / ListView>
< / Grid>

当我调试此应用程序时,当我检查/取消选中复选框时,它不会到达设置者的行。
这段代码有什么问题吗?
提前感谢

解决方案

将CheckBox绑定到IsSelected属性。此属性实现为自动实现的属性。你永远不会在调试器中的setter或getter断开。我的代码中看不到任何问题,它应该像编码一样工作。


I want to make two way binding for checkboxes inside ListView. This is my Product class:

public class Product
{
    public bool IsSelected { get; set; }
    public string Name { get; set; }
}

In ViewModel class I have observable collection of products:

    private ObservableCollection<Product> _productList;
    public ObservableCollection<Product> ProductList
    {
        get
        {
            return _productList;
        }
        set
        {
            _productList = value;
        }
    }

    public MainViewModel()
    {
        ProductList = new ObservableCollection<Product>
                          {
                              new Product {IsSelected = false, Name = "Not selected"},
                              new Product {IsSelected = true, Name = "Selected"},
                              new Product {IsSelected = true, Name = "Selected"}
                          };
    }
}

And finally I have Grid with ListView that binding my ProductList:

<Grid>
    <ListView Height="120" HorizontalAlignment="Left" 
                  VerticalAlignment="Top"
                  SelectionMode="Multiple" 
                  ItemsSource="{Binding ProductList}" >
        <ListView.View>
            <GridView>
                <GridViewColumn Width="40">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Width="120" Header="Product Name" DisplayMemberBinding="{Binding Path=Name}" />
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

When I debug this app it never arrive to setter's line when I check/uncheck checkboxes. Any ideas what is wrong in this code? Thanks in advance!

解决方案

You bound the CheckBox to the IsSelected property. This property is implemented as an auto implemented property. You will never break at the setter or getter in debugger. I can't see any issue in your code, it should work like you've coded it.

这篇关于CheckBox双向绑定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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