Come collego il membro IsChecked di un CheckBox a una variabile membro nel mio modulo?WPF Databinding CheckBox.IsChecked
(mi rendo conto che posso accedervi direttamente, ma sto cercando di conoscere l'associazione dati e WPF)
Qui di seguito è il mio tentativo fallito per ottenere questo lavoro.
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>
Codice:
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
}
}
}
Se la proprietà è in un 'ViewModel' piuttosto che nel' View' stesso, come faresti il binding? – Pat
Se si utilizza un ViewModel, in genere si imposta DataContext all'interno della vista (o in XAML) sul ViewModel e si esegue semplicemente 'IsChecked =" {Binding ShowPending} "' –