WPF数据绑定CheckBox.IsChecked

前端之家收集整理的这篇文章主要介绍了WPF数据绑定CheckBox.IsChecked前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何将CheckBox的IsChecked成员绑定到表单中的成员变量?

(我意识到我可以直接访问它,但我正在尝试学习数据绑定和WPF)

以下是我尝试让这个工作失败的尝试.

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>

注意我添加了一个名称< Window>,并更改了CheckBox中的绑定.如果您希望它在更改时能够更新,您还需要将ShowPending实现为DependencyProperty.

原文链接:https://www.f2er.com/html/226547.html

猜你在找的HTML相关文章