我有TextBox
,其Text
属性绑定到名为double
的{{1}}类型的属性。我还有一个Grade
,当CheckBox
被选中时,我希望CheckBox
采用自动计算的值(即自动设置为MaxScore / Count of questions)。如果未选中Grade
,那么我想手动设置和更改CheckBox
。我的问题是我该如何实现?
Grade
我把它绑定到:
<TextBox Height="23"
Visibility="{Binding Path=Visible2, Converter={StaticResource boolToVis}, UpdateSourceTrigger=PropertyChanged}"
Text="{Binding Path=Grade,UpdateSourceTrigger =PropertyChanged,Mode=TwoWay}"
HorizontalAlignment="Left" Margin="376,453,0,0"
Name="textBox3" VerticalAlignment="Top" Width="120" />
由于
答案 0 :(得分:0)
在您的虚拟机中,拥有另一个公共属性自动并确保您的复选框绑定到它,您的TextBox也是如此(以便它成为只读/禁用/隐藏)。我的默认AutoCheck将是真的
答案 1 :(得分:0)
<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
<Label Content="Grade" />
<TextBox Height="23" Width="120"
Text="{Binding Path=GradeDisplay}"
IsEnabled="{Binding EnableBox}"
/>
<Label Content="Auto?" />
<CheckBox IsChecked="{Binding IsAuto}" />
</StackPanel>
对于View Model
public bool EnableBox { get; set; }
public string GradeDisplay
{
get
{
if (EnableBox)
return Grade.ToString();
else
return "AUTO";
}
set
{
double result;
if (double.TryParse(value, out result))
Grade = result;
NotifyPropertyChanged("GradeDisplay");
}
}
private bool _IsAuto;
public bool IsAuto
{
get
{
return _IsAuto;
}
set
{
_IsAuto = value;
EnableBox = !value;
NotifyPropertyChanged("GradeDisplay");
NotifyPropertyChanged("EnableBox");
}
}
private double _Grade;
private double Grade
{
set
{
_Grade = value;
}
get
{
if (IsAuto)
{
// CODE TO GET AUTO GRADE
return 0.0;
}
else
{ // RETURN MANUALLY SET GRADE
return _Grade;
}
}
}
// CTOR
public MainWindowViewModel()
{
IsAuto = false;
}