数据绑定在WPF C#中不起作用

时间:2014-08-14 10:52:18

标签: c# wpf xaml data-binding

我想问一下ObservableCollection是否有类似于WPF C#中的对象。

这是我的问题:

我在XAML中关注Grid

<Grid x:Name="detailsGrid">
   <TextBox Text="{Binding Name, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" />
   <TextBox Text="{Binding Excerpt, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" />
</Grid>

在我的代码后面,我将detailsGrid的DataContext设置为以下类的对象:

recipeDetails = new RecipeDetailsContainer();
this.detailsGrid.DataContext = recipeDetails;

RecipeDetailsContainer上课:

public class RecipeDetailsContainer : INotifyPropertyChanged
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set 
        { 
            _name = value;
            NotifyPropertyChanged("Name");
        }
    }

    private string _excerpt;
    public string Excerpt
    {
        get { return _excerpt; }
        set
        {
            _excerpt = value;
            NotifyPropertyChanged("Excerpt");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

在其他类方法中,我想将此信息写入WPF窗口,所以我设置:

private void PrintInfoAboutRecipe(object sender, RecipeHandlerArgs e)
{
    this.Dispatcher.Invoke((Action)(() => 
    {
        if (e.Container != null && e.Container.Recipe != null)
        {
            this.recipeDetails.Name = e.Container.Recipe.Name;
            this.recipeDetails.Excerpt = e.Container.Recipe.Excerpt;
        }
        else
        {
            this.recipeDetails.Name = "";
            this.recipeDetails.Excerpt = "";
        }
    }));
}

但我的绑定不起作用,TextBlocks中没有显示任何内容。

我做错了吗?任何帮助,将不胜感激。谢谢! : - )

2 个答案:

答案 0 :(得分:1)

<Grid x:Name="detailsGrid">
   <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto" />
        <ColumnDefinition Width="auto" />
   </Grid.ColumnDefinitions>
   <TextBox  Text="{Binding Name}" />
   <TextBox Grid.Column="1" Text="{Binding Excerpt}" />
</Grid>

尝试在列中添加TextBox,因为它们位于同一位置。其次尝试在类构造函数中初始化属性。

public class RecipeDetailsContainer : INotifyPropertyChanged
{
  public RecipeDetailsContainer() {
    Name = string.Empty;
    Excerpt = string.Empty;
  }

有关BindingMode的更多信息,请访问:

Binding.Mode

<强>分派器

可能您必须Dispatcher.Current.Invoke()您的属性集。如果从不同的线程设置它们,则绑定失败。

答案 1 :(得分:1)

假设datacontext,vm等是正确的:

的Xaml:

<Grid>
  <Grid.RowDefinitons>
      <RowDefinition Height="Auto"/>
      <RowDefinition Height="Auto"/>
      <RowDefinition Height="*"/> <!-- empty space, not sure how you want your gui
        but your boxes would overlap in your example -->
  </Grid.RowDefinitions>
  <!-- updates binding on vm/datacontext on each keypress -->
  <TextBox Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> 
  <!-- updates binding on vm/datacontext when focus is lost -->
  <TextBox Grid.Row="1" Text="{Binding Excerpt, Mode=TwoWay" />
</Grid>

UpdateSourceTrigger = PropertyChanged有点偏执,默认是绑定在文本框失去行为后更新您的viewmodel。从我的例子中我知道的一点点我就放弃了。双向绑定在控制中非常重要,您希望能够从GUI和VM更新属性/ DP。

你有一个名为the WPF cheatsheet的东西,它已经很老了但你在一个非常紧凑的地方最需要绑定。在学习WPF时,我的文件粘在电脑后面的墙上@ work,home&amp;学习WPF的学校。

希望它有所帮助,

干杯,

了Stian