更改CheckBox.IsChecked后,CheckBox.IsChecked已绑定的DataTrigger源值不会改变。
我有简单的ViewModel
public class ViewModel : INotifyPropertyChanged
{
private bool check1;
public bool Check1
{
get { return check1; }
set { check1 = value; NotifyPropertyChanged(); }
}
private bool check2;
public bool Check2
{
get { return check2; }
set { check2 = value; NotifyPropertyChanged(); }
}
#region Notify
...
#endregion
}
和简单的XAML
<StackPanel Grid.Row="1">
<CheckBox Content="Check1">
<CheckBox.Style >
<Style TargetType="{x:Type CheckBox}">
<Setter Property="IsChecked" Value="{Binding Check1, Mode=TwoWay}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Check2}" Value="True">
<Setter Property="IsChecked" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>
<CheckBox Content="Check2" IsChecked="{Binding Check2}"/>
<TextBlock Text="{Binding Check1}" Name="uiCheckValue1"/>
<TextBlock Text="{Binding Check2}" Name="uiCheckValue2"/>
</StackPanel>
答案 0 :(得分:2)
您的代码似乎有误。您应该正确发出PropertyChanged
事件以更新视图。
检查以下代码:
public class ViewModel : INotifyPropertyChanged
{
private bool check1;
public bool Check1
{
get { return check1; }
set
{
check1 = value;
PropertyChanged(value, new PropertyChangedEventArgs("Check1"));
}
}
private bool check2;
public bool Check2
{
get { return check2; }
set
{
check2 = value;
PropertyChanged(value, new PropertyChangedEventArgs("Check1"));
}
}
#region Notify
...
#endregion
}
同时将绑定更改为TwoWay
<<CheckBox Content="Check2" IsChecked="{Binding Check2, Mode=TwoWay}"/>
<DataTrigger Binding="{Binding Check2, Mode=TwoWay}" Value="True">
<Setter Property="IsChecked" Value="True" />
</DataTrigger>
答案 1 :(得分:1)
很容易说出原因:如果您手动设置IsChecked
True
,则会失去绑定。
删除DataTrigger
并更改您的ViewModel,如下所示:
public bool Check2{
get { return check2; }
set {
check2 = value;
if (value == true) Check1 = true;
NotifyPropertyChanged();
}
}
答案 2 :(得分:0)
有点晚了,但是如果您改变了
var data = {
"test":[1,2,3]
}
$.ajax({
url : 'API/url',
type : 'POST',
data : data,
dataType:'json',
success: function(){do stuff}
});
收件人
<CheckBox Content="Check2" IsChecked="{Binding Check2}"/>
您可以摆脱后面的所有代码。复选框的默认值类似于“ LostFocus”,这很烦人。