【问题标题】:Dependency Property won't update after changing datacontext更改数据上下文后,依赖属性不会更新
【发布时间】:2012-04-14 16:23:53
【问题描述】:

我有一个与数据上下文绑定的 WPF 文本框。

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

我在文本框的容器控件(本例中为tabItem)的代码中设置了datacontext

tiMaterial.DataContext = _materials[0];

我还有一个包含其他材料的列表框。 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;            
}

Material 类实现了INotifyPropertyChanged 接口。我有双向更新工作,只是当我更改 DataContext 时,绑定似乎丢失了。

我错过了什么?

【问题讨论】:

    标签: c# wpf binding datacontext


    【解决方案1】:

    我尝试按照您在帖子中描述的操作,但真诚地我没有发现问题。在我测试的所有情况下,我的项目都运行良好。 我不喜欢你的解决方案,因为我认为 MVVM 更清晰,但你的方式也行得通。

    希望对你有帮助。

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多