Vincent
Vincent

Reputation: 941

Dependency Property won't update after changing datacontext

I have a WPF textbox with a binding to the datacontext.

<TextBox Grid.Column="1" Grid.Row="4" Text="{Binding Path=Density,UpdateSourceTrigger=PropertyChanged}"/>

I set the datacontext in the code of a the container control of the textbox (tabItem in this case)

tiMaterial.DataContext = _materials[0];

I also have a listbox with other materials. I want to update the textfield, when another material is selected, hence I code:

private void lbMaterials_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
    _material = (Material) lbMaterials.SelectedValue;
    tiMaterial.DataContext = _material;            
}

The Material class implements the INotifyPropertyChanged interface. I have the two-way update working, it's just when I change the DataContext, the bindings seem to be lost.

What am I missing?

Upvotes: 2

Views: 1167

Answers (1)

pluka
pluka

Reputation: 125

I tryed to do what you describe in your post but sincerely I didn’t find the problem. In all the cases that I tested my project works perfectly. I don’t like your solution because I think that MVVM is more clear, but your way works too.

I hope this helps you.

public class Material
{
    public string Name { get; set; }    
}

public class ViewModel : INotifyPropertyChanged
{
    public ViewModel()
    {
        Materials = new Material[] { new Material { Name = "M1" }, new Material { Name = "M2" }, new Material { Name = "M3" } };
    }

    private Material[] _materials;
    public Material[] Materials
    {
        get { return _materials; }
        set { _materials = value;
            NotifyPropertyChanged("Materials");
        }
    }

    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion
}

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        DataContext = new ViewModel();
    }

    private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        gridtext.DataContext = (lbox.SelectedItem);
    }
}

.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <Grid x:Name="gridtext">
        <TextBlock Text="{Binding Name}" />
    </Grid>

    <ListBox x:Name="lbox" 
             Grid.Row="1"
             ItemsSource="{Binding Materials}"
             SelectionChanged="ListBox_SelectionChanged">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

Upvotes: 1

Related Questions