Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TwoWay-Binding does not work

Tags:

c#

.net

binding

wpf

the XAML of my window:

<ListView Grid.Row="0" Name="files">
        <ListView.Resources>
            <DataTemplate x:Key="CheckboxTemplate">
                <CheckBox IsChecked="{Binding Save, Mode=TwoWay}" />
            </DataTemplate>
        </ListView.Resources>
        <ListView.View>
            <GridView AllowsColumnReorder="False">
                <GridViewColumn Header=" " Width="30" CellTemplate="{StaticResource CheckboxTemplate}" />
                <GridViewColumn Header="Datei" DisplayMemberBinding="{Binding File}"/>
            </GridView>
        </ListView.View>
    </ListView>

the constructor of my Window:

IEnumerable<SaveItem> sil = sdl.Select(d => new SaveItem() { Save = true, Document = d });
files.ItemsSource = sil;

and the datastructure i want to display:

public class SaveItem : INotifyPropertyChanged
{
    private bool save;
    public bool Save
    {
        get { return this.save; }

        set
        {
            if (value != this.save)
            {
                this.save = value;
                NotifyPropertyChanged("Save");
            }
        }
    }

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
    public StandardDocument Document { get; set; }
    public string File { get { return Document.Editor.File; } }

    #region INotifyPropertyChanged Member

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}

i call the window. The window appears. I uncheck a checkbox of an item of the listview. i click a button. in its event-handler i read out the itemssource of the listview and ... the Save-Property of the Unchecked Item is (in its source) still true!

where is my mistake? why does my sources not get updated if i check/uncheck a checkbox?

like image 586
0xDEADBEEF Avatar asked Aug 19 '26 04:08

0xDEADBEEF


1 Answers

You have not set your data context. If you are all in the same class - put something like this in your constructor of the window.

DataContext = this;
like image 192
tsells Avatar answered Aug 21 '26 19:08

tsells



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!