WPF 数据绑定 CheckBox.IsChecked [英] WPF Databinding CheckBox.IsChecked
本文介绍了WPF 数据绑定 CheckBox.IsChecked的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将 CheckBox 的 IsChecked 成员绑定到表单中的成员变量?
How would I bind the IsChecked member of a CheckBox to a member variable in my form?
(我意识到我可以直接访问它,但我正在尝试了解数据绑定和 WPF)
(I realize I can access it directly, but I am trying to learn about databinding and WPF)
以下是我尝试使其正常工作的失败尝试.
Below is my failed attempt to get this working.
XAML:
<Window x:Class="MyProject.Form1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Title" Height="386" Width="563" WindowStyle="SingleBorderWindow">
<Grid>
<CheckBox Name="checkBoxShowPending"
TabIndex="2" Margin="0,12,30,0"
Checked="checkBoxShowPending_CheckedChanged"
Height="17" Width="92"
VerticalAlignment="Top" HorizontalAlignment="Right"
Content="Show Pending" IsChecked="{Binding ShowPending}">
</CheckBox>
</Grid>
</Window>
代码:
namespace MyProject
{
public partial class Form1 : Window
{
private ListViewColumnSorter lvwColumnSorter;
public bool? ShowPending
{
get { return this.showPending; }
set { this.showPending = value; }
}
private bool showPending = false;
private void checkBoxShowPending_CheckedChanged(object sender, EventArgs e)
{
//checking showPending.Value here. It's always false
}
}
}
推荐答案
<Window ... Name="MyWindow">
<Grid>
<CheckBox ... IsChecked="{Binding ElementName=MyWindow, Path=ShowPending}"/>
</Grid>
</Window>
注意,我为
添加了一个名称,并更改了 CheckBox 中的绑定.您需要将 ShowPending 实现为 DependencyProperty
如果您希望它能够在更改时更新.
Note i added a name to <Window>
, and changed the binding in your CheckBox. You will need to implement ShowPending as a DependencyProperty
as well if you want it to be able to update when changed.
这篇关于WPF 数据绑定 CheckBox.IsChecked的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文