日期:2014-05-16  浏览次数:20867 次

WPF,这种情况下的绑定怎么做的?
本帖最后由 u013837558 于 2014-03-21 23:37:20 编辑
有一个UserControl。如下:

<UserControl x:Class="WPF2.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid Height="80" Width="220" Background="Yellow">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <CheckBox Content="CheckBox" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        <CheckBox Content="CheckBox" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        <CheckBox Content="CheckBox" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        <CheckBox Content="CheckBox" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>
</UserControl>


这个UserControl由4个CheckBox组成,并有一个Int型的属性SelectedNumber,顾名思义就是CheckBox的勾选个数,假设只有一个CheckBox勾选,SelectedNumber属性值就为1,假设有两个个CheckBox勾选,SelectedNumber属性值就为2,以此类推。
现在我想实现这样一个功能:任何一个CheckBox的勾选状态发生变化时,SelectedNumber属性值自动发生变化,请问该怎么去做?是不是在CheckBox的Click事件中去实现呢,除此之外,还有没有其它方法呢?思路是怎么样的?
------解决方案--------------------
 public class Myclass 
    {
        static CheckBox checkBox1 = new CheckBox();
        static CheckBox checkBox2 = new CheckBox();
        static CheckBox checkBox3 = new CheckBox();
        static CheckBox checkBox4 = new CheckBox();
        public int SelectedNumber  //只读属性
        {
           &nb