Как связать член IsChecked из CheckBox с переменной-членом в моей форме?WPF Databinding 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
}
}
}
Если свойство является в 'ViewModel', а не в' View', как бы вы сделали привязку? – Pat
Если вы используете ViewModel, вы обычно устанавливаете свой DataContext в своем представлении (или в XAML) в ViewModel и просто выполняете 'IsChecked = {{Binding ShowPending}" ' –