WPF Databinding CheckBox.IsChecked

How would I bind the IsChecked member of a CheckBox to a member variable in my form?

(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>

Code:

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 } } }

--------------Solutions-------------

<Window ... Name="MyWindow">
<Grid>
<CheckBox ... IsChecked="{Binding ElementName=MyWindow, Path=ShowPending}"/>
</Grid>
</Window>

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.

Addendum to @Will's answer: this is what your DependencyProperty might look like (created using Dr. WPF's snippets):

#region ShowPending

/// <summary>
/// ShowPending Dependency Property
/// </summary>
public static readonly DependencyProperty ShowPendingProperty =
DependencyProperty.Register("ShowPending", typeof(bool), typeof(MainViewModel),
new FrameworkPropertyMetadata((bool)false));

/// <summary>
/// Gets or sets the ShowPending property. This dependency property
/// indicates ....
/// </summary>
public bool ShowPending
{
get { return (bool)GetValue(ShowPendingProperty); }
set { SetValue(ShowPendingProperty, value); }
}

#endregion

Category:wpf Time:2009-06-18 Views:1

Related post

  • WPF Bind Checkbox IsChecked to other Checkboxes (IsChecked || !IsEnabled) 2010-12-14

    I have a select 'All' checkbox that I would like to be bound to the (IsChecked || !IsEnabled) of other checkboxes. Checking 'All' checks all enabled checkboxes. Unchecking 'All' unchecks all checkboxes. Manually checking each enabled checkbox will ch

  • WPF checkbox IsChecked property does not change according to binding value 2009-09-03

    here is my code: xaml side: I use a data template to bind with item "dataType1" <DataTemplate DataType="{x:Type dataType1}"> <WrapPanel> <CheckBox IsChecked="{Binding Path=IsChecked, Mode=TwoWay}" Command="{Binding Path=CheckedCommand}

  • WPF How to set checkbox IsChecked binding in code behind 2011-02-11

    I have a style that I have to create in code-behind. It has a checkbox that looks like this.. <CheckBox HorizontalAlignment="Center" VerticalAlignment="Center" IsChecked="{Binding Path=DataItem.IsChecked}" > </CheckBox> How do I replicate

  • WPF CheckBox IsChecked can't be reset when page(data source) is updated 2010-02-26

    I have question on the checkbox. First of all, I have a usercontrol which has a list box like this and this user control will be switched by 2 button and then the data source is changed and then the the displayed officer status will be changed: When

  • WPF Databinding Calculated Value sometimes minutes sometimes hours 2009-07-01

    I have a WPF window with a textbox, using standard WPF Databinding to an object. This all works fine, the issue is this: The user is entering a time estimate, I would like to give the user the option to enter the data in hours or minutes (through a d

  • What makes WPF databinding so powerful? 2009-07-26

    Just a simple question really, might be to simple really but hey here goes. What makes WPF binding so powerful vs say Winforms databinding? What are you likes and dislikes about it? Any code example that show it's power would be cool. --------------S

  • WPF DataBinding to Flag Enum (in a PropertyGrid) 2009-09-24

    I need to have the ability to select multiple values as is the nature of a Flag enumeration from a WPF view (all be it, in a PropertyGrid). The properties in question are dynamic and no pre-defined DataTemplates can be used as the type of the propert

  • wpf listbox checkbox combination, really stuck 2011-02-09

    I am trying to link the checkbox and listview together, and then use the binding method to set the path to the object in order to set the check-box IsChecked status in the listbox. List<Test1> datas = new List<Test1>(); var data = new Tes

  • Logging value changes in WPF Databinding to a Property 2011-04-27

    I have a question about WPF databinding notification I have a checkbox which is two-way binded to a bool propery of an internal data model. What I want is to show a value changed message in the UI (let's say "SomeBoolParameter changes from true =>

  • Binding ribbon text box IsEnabled to checkbox IsChecked not working 2012-02-03

    Here is the sample XAML: ... <ribbon:RibbonTab Header="MyTab"> <ribbon:RibbonGroup Header="Blah"> <ribbon:RibbonTextBox x:Name="MyTextBox" IsEnabled="{Binding IsChecked, ElementName=MyCheckBox}" /> <ribbon:RibbonCheckBox x:Name="

  • WPF Databinding and cascading Converters? 2008-10-17

    i wonder if it is possible to cascade converters when using wpf databinding. e.g. something like <SomeControl Visibility="{Binding Path=SomeProperty, Converter={StaticResource firstConverter}, Converter={StaticResource secondConverter}}"/> is i

  • Does any one know of an exhaustive collection of WPF databinding examples? 2009-04-27

    It seems that every time I read an article on "how to do WPF data binding", it is done with some different variation, sometimes with DataContext, sometimes without, sometimes with Itemssource or both Itemssource and DataContext, there's also ObjectDa

  • How to get WPF DataBinding-to-an-object to work 2009-05-11

    In the following example I bind the XAML to a static object via ObjectDataProvider. When the user changes information, I want it to automatically reflect in the XAML. What I don't understand is: how do I perpetuate the object? do I have to create a s

  • WPF Databinding intellisense 2009-06-03

    Ok, so I have a general question about WPF. I've messed a little with ASP.NET MVC and in the markup it has intellisense on your viewmodel object so you don't mistype it( i love it). Enter WPF, I love it, I'm utilizing the MVVM approach and one annoyi

  • Some Problem with WPF Databinding but You Have to Guess What Kind 2009-07-09

    I'm trying to implement a property changed event (or which ever is appropriate) in my wpf project and I'm trying to find the best way to do this. I have this header where it has an expand/collapse icon and in my window, I might have multiple headers.

  • Does WPF databinding marshall changes to the UI Thread? 2009-08-24

    I just noticed that when changing bound properties in my ViewModel (MVVM) from a background worker thread I do not get any exceptions and the view is updated correctly. Does this mean I can safely rely on wpf databinding marshalling all changes in th

  • Default values in WPF DataBinding? 2010-02-10

    Can we set default values in WPF DataBinding? e.g : I've bound the Height and Width of my window to some properties as follows. <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2

  • ListBox CheckBox IsChecked binding using MVVM 2010-02-18

    I'm trying to figure out a way to bind my CheckBox IsChecked property. Basically, I have a list of items which the ListBox is bound to. When a user checks the box, a command is invoked and that item is added to a collection. However, what if I want t

  • WPF DataBinding, CollectionViewSource, INotifyPropertyChanged 2010-03-11

    First time when I tried to do something in WPF, I was puzzled with WPF DataBinding. Then I studied thorougly next example on MSDN: http://msdn.microsoft.com/en-us/library/ms771319(v=VS.90).aspx Now, I quite understand how to use Master-Detail paradig

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.538 (s). 13 q(s)