我正在尝试将我的复选框绑定到公共布尔属性,但我无法使其工作。
我错过了什么吗?
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<CheckBox Name="CheckBox2" IsChecked="{Binding CheckBoxState}">
</CheckBox> </Grid>
代码背后的代码:
public const string NamePropertyName = "CheckBoxState";
private bool _checkboxstate = true;
public bool CheckBoxState
{
get { return _checkboxstate; }
set
{
if (_checkboxstate == value) return;
_checkboxstate = value;
RaisePropertyChanged(NamePropertyName);
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
void appBarButtonSelect_Click(object sender, EventArgs e)
{
// HERE IT SHOULD TOGGLE STATE BUT IT DOES NOT!
CheckBoxState = !CheckBoxState;
}
答案 0 :(得分:0)
答案隐藏在表面下方七层。我真的在检查每一个wpf绑定问题,以达到答案,最终我找到了它!我开始认为WP8问题比WPF更多。
所以我必须绑定我的窗口的datacontext,如
<phone:PhoneApplicationPage DataContext="{Binding RelativeSource={RelativeSource Self}}">
回答here
我需要更多关于wpf绑定的阅读:)