【问题标题】:WPF - Can't display ObservableCollection in a windowWPF - 无法在窗口中显示 ObservableCollection
【发布时间】:2010-01-09 18:45:44
【问题描述】:

我有一个似乎无法在窗口中显示的 ObservableCollection。代码如下:

视图模型:

public class QueryParamsVM : DependencyObject
{
    public string Query { get; set; }

    public QueryParamsVM()
    {
        Parameters = new ObservableCollection<StringPair>();
    }

    public ObservableCollection<StringPair> Parameters
    {
        get { return (ObservableCollection<StringPair>)GetValue(ParametersProperty); }
        set { SetValue(ParametersProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Parameters.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ParametersProperty =
        DependencyProperty.Register("Parameters", typeof(ObservableCollection<StringPair>), typeof(QueryParamsVM), new UIPropertyMetadata(null));
}


public class StringPair: INotifyPropertyChanged
{
    public string Key { get; set; }
    public string Value
    {
        get { return _value; }
        set { _value = value; OnPropertyChanged("IsVisible"); }
    }
    private string _value;

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

窗口 xaml:

<Window x:Class="WIAssistant.QueryParams"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Query Parameters" Height="390" Width="504">

    <Grid>
        <ListBox DataContext="{Binding Parameters}" >

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Key}"  ></TextBlock>
                        <TextBlock Text="{Binding Value}"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>        
    </Grid>
</Window>

调用代码:

 // Get Project Parameters
 QueryParams queryParams = new QueryParams();
 queryParams.ViewModel.Parameters.Add(new StringPair {Key = "@project", Value = ""});
 queryParams.ShowDialog();

我尝试将调试语句放入绑定中。 Parameter 被绑定,但 Value 绑定永远不会被调用。

关于如何让我的列表显示的任何想法?

【问题讨论】:

  • 检查调试输出窗口,看看是否有任何绑定问题。我想知道您是否需要 {Binding Path=xxx} 而不是 {Binding xxx}。
  • 如果只指定一个参数,则不需要添加“Path="..?
  • 感谢这两个答案。我将解决这两个优点。

标签: wpf data-binding collections binding


【解决方案1】:

试试

<ListBox ItemsSource="{Binding Parameters}">

<ListBox DataContext="{Binding Parameters}" ItemsSource="{Binding}">

【讨论】:

    【解决方案2】:

    这里有点奇怪:

    public string Value
    {
        get { return _value; }
        set { _value = value; OnPropertyChanged("IsVisible"); }
    }
    

    您正在针对不同的属性引发属性更改事件。应该是:

    public string Value
    {
        get { return _value; }
        set { _value = value; OnPropertyChanged("Value"); }
    }
    

    【讨论】:

    • 好点。我会解决这个问题(这是复制和粘贴的危险)。然而问题是由于斯坦尼斯拉夫指出的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    • 2019-02-20
    相关资源
    最近更新 更多