将复选框绑定到updatesourcepropery

时间:2015-02-14 14:13:54

标签: xaml

我有一个简单的类,我有一个可观察的集合:

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");
            }
        }

在我的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-bool时,我希望我的程序做出反应。

我试过例如:

<CheckBox IsChecked="{Binding HasSeatbelt, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

但它似乎不足以触发更新事件。关于这个的任何提示?

1 个答案:

答案 0 :(得分:0)

当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:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <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, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                    </StackPanel>
                </Border>

            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

代码背后:

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;
}
}