Xaml 将复选框绑定到UpdateSourceProperty
我有一个简单的类,我有一个可观察的集合:Xaml 将复选框绑定到UpdateSourceProperty,xaml,Xaml,我有一个简单的类,我有一个可观察的集合: public class Car { public int Id { get; set; } public string Brand { get; set; } public bool HasSeatbelt { get; set; } } private ObservableCollection<Car> _cars;
public class Car
{
public int Id { get; set; }
public string Brand { get; set; }
public bool HasSeatbelt { get; set; }
}
private ObservableCollection<Car> _cars;
public ObservableCollection<Car> Cars
{
get { return _cars; }
set
{
_cars = value;
RaisePropertyChanged("Cars");
}
}
公车
{
公共int Id{get;set;}
公共字符串品牌{get;set;}
公共布尔HasSeatbelt{get;set;}
}
私人可观测收集车;
公共可观测收集车
{
获取{return_cars;}
设置
{
_汽车=价值;
RaisePropertyChanged(“汽车”);
}
}
在我的XAML中,我有以下内容:
<Button.Flyout>
<Flyout>
<ListView
Background="Azure"
x:Name="ContactList"
ItemsSource="{Binding Path=Cars}"
SelectedItem="{Binding SelectedCar, Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate>
<Border Width="300" Height="Auto" BorderThickness="1">
<StackPanel>
<TextBlock>
<Run Text="{Binding Brand}" />
</TextBlock>
<CheckBox IsChecked="{Binding HasSeatbelt}"/>
</StackPanel>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Flyout>
</Button.Flyout>
这将向用户显示车辆列表以及有关安全带状况的复选框。我希望我的程序在用户点击复选框时做出反应,并将其设置为HasSeatbelt布尔值
例如,我尝试过:
<CheckBox IsChecked="{Binding HasSeatbelt, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
但这似乎不足以触发更新事件。关于这一点有什么提示吗?当HasSeatBelt的值更改时,会引发PropertyChanged事件,以便通知您的绑定
public class Car: INotifyPropertyChanged
{
private bool hasSeatBelt;
public bool HasSeatbelt {
get {return hasSeatBelt; }
set {
hasSeatBelt=value;
OnPropertyChanged("HasSeatbelt");
}
}
//Same for other properties
void OnPropertyChanged(string p)
{
if(PropertyChanged!=null)
{
PropertyChanged(this, new PropertyChangedEventArgs(p));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
我刚刚创建了一个小示例,只需将其复制粘贴到一个新项目中,就可以看到它工作了。(这不是MVVM,但我相信您会让它为MVVM工作)
XAML:
代码隐藏:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window,INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
Cars = new ObservableCollection<Car>();
Cars.Add(new Car { Brand = "Ford", HasSeatbelt = true, Id = 1 });
Cars.Add(new Car { Brand = "Mazda", HasSeatbelt =false, Id = 2 });
}
private ObservableCollection<Car> _cars;
public ObservableCollection<Car> Cars
{
get { return _cars; }
set
{
_cars = value;
RaisePropertyChanged("Cars");
}
}
void RaisePropertyChanged(string p)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(p));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class Car : INotifyPropertyChanged
{
public int Id { get; set; }
public string Brand { get; set; }
private bool hasSeatBelt;
public bool HasSeatbelt
{
get { return hasSeatBelt; }
set
{
hasSeatBelt = value;
OnPropertyChanged("HasSeatbelt");
MessageBox.Show(value.ToString());
}
}
//Same for other properties
void OnPropertyChanged(string p)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(p));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用System.Collections.ObjectModel;
使用系统组件模型;
命名空间WpfApplication1
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口,INotifyPropertyChanged
{
公共主窗口()
{
初始化组件();
this.DataContext=this;
Cars=新的可观测集合();
添加(新车{Brand=“Ford”,HasSeatbelt=true,Id=1});
添加(新车{Brand=“Mazda”,HasSeatbelt=false,Id=2});
}
私人可观测收集车;
公共可观测收集车
{
获取{return_cars;}
设置
{
_汽车=价值;
RaisePropertyChanged(“汽车”);
}
}
void raiseProperty已更改(字符串p)
{
if(PropertyChanged!=null)
{
房地产变更(这是新的房地产变更发展(p));
}
}
公共事件属性更改事件处理程序属性更改;
}
公共级汽车:InotifyProperty已更改
{
公共int Id{get;set;}
公共字符串品牌{get;set;}
私人布尔哈赛特带;
公共布尔哈赛特带
{
获取{return hasSeatBelt;}
设置
{
hasSeatBelt=值;
不动产变更(“HasSeatbelt”);
Show(value.ToString());
}
}
//其他属性也一样
void OnPropertyChanged(字符串p)
{
if(PropertyChanged!=null)
{
房地产变更(这是新的房地产变更发展(p));
}
}
公共事件属性更改事件处理程序属性更改;
}
}
您需要在CarThank上实现INotifyPropertyChanged。如果需要,它将通过RaisePropertyChanged实现。您需要为bool属性和任何其他属性引发事件,并感谢您的回答。我正在尝试更改列表中某项的属性。我正在使用MVVM。我试着将你的代码添加到我的汽车类中,但没有成功。(未触发事件)。我想也许我需要某种bool-converter。你不需要bool-converter,请用新代码更新你的答案试试这个例子@bugsy谢谢!我在和你玩过之后就开始工作了!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window,INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
Cars = new ObservableCollection<Car>();
Cars.Add(new Car { Brand = "Ford", HasSeatbelt = true, Id = 1 });
Cars.Add(new Car { Brand = "Mazda", HasSeatbelt =false, Id = 2 });
}
private ObservableCollection<Car> _cars;
public ObservableCollection<Car> Cars
{
get { return _cars; }
set
{
_cars = value;
RaisePropertyChanged("Cars");
}
}
void RaisePropertyChanged(string p)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(p));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class Car : INotifyPropertyChanged
{
public int Id { get; set; }
public string Brand { get; set; }
private bool hasSeatBelt;
public bool HasSeatbelt
{
get { return hasSeatBelt; }
set
{
hasSeatBelt = value;
OnPropertyChanged("HasSeatbelt");
MessageBox.Show(value.ToString());
}
}
//Same for other properties
void OnPropertyChanged(string p)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(p));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}