silverlight复选框中的绑定值

本文关键字:绑定 复选框 silverlight | 更新日期: 2023-09-27 18:04:23

我在xaml文件中有3个复选框。名坐、站、睡眠。具有以下值1、2、3的Extype

CheckBox Content="Sit" Margin="127,89,212,136" IsChecked="{Binding Extype}" RenderTransformOrigin="1.817,-1.029"/>
CheckBox Content="Stand" Margin="127,89,212,136" IsChecked="{Binding Extype}" RenderTransformOrigin="1.817,-1.029"/>
CheckBox Content="Sleep" Margin="127,89,212,136" IsChecked="{Binding Extype}" RenderTransformOrigin="1.817,-1.029"/>

如果Extype值为,则表示我需要选中"Sit"复选框。

如果Extype值为两个表示我需要选择站立复选框。

如果Extype值为三个表示我需要选中两个复选框。

我该怎么做?

silverlight复选框中的绑定值

XAML:

<CheckBox Content="Sit" IsChecked="{Binding IsSit, Mode=OneWay}" IsEnabled="False"/>
<CheckBox Content="Stand" IsChecked="{Binding IsStand, Mode=OneWay}" IsEnabled="False"/>
<CheckBox Content="Sleep" IsChecked="{Binding IsSleep, Mode=OneWay}" IsEnabled="False"/>

ViewModel:

public bool IsSit
{
    get
    {
        return ExtType == 1 || ExtType == 3;
    }
}
public bool IsStand
{
    get
    {
        return ExtType == 2 || ExtType == 3;
    }
}
private int _extType;
public int ExtType
{
    get
    {
        return _extType;
    }
    set
    {
        _extType = value;
        RaisePropertyChanged("IsSit");
        RaisePropertyChanged("IsStand");
    }
}

可以使用带参数的ValueConverter。在每个复选框中使用相同的ValueConverter,但更改参数值。

<CheckBox Content="Sit" IsChecked="{Binding Extype, Converter={StaticResource YourConverter}, ConverterParameter=Sit}" />
<CheckBox Content="Standup" IsChecked="{Binding Extype, Converter={StaticResource YourConverter}, ConverterParameter=Standup}" />
下面是一个带参数的ValueConverter的例子:http://wpftutorial.net/ValueConverters.html

(记住将ValueConverter添加为资源)

然后将您的业务逻辑代码放入ValueConverter中,或者甚至更好,调用业务层中的函数。