views:

118

answers:

2

I have an ObservableCollection that I can't seem to get to display in a window. Here is the code:

The View Model:

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));
    }
}

The Window 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>

The calling code:

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

I have tried putting debug statements in the bindings. The Parameter gets bound to, but the Value binding never gets called.

Any Ideas on how to get my list to show up?

+3  A: 

Try

<ListBox ItemsSource="{Binding Parameters}">

or

<ListBox DataContext="{Binding Parameters}" ItemsSource="{Binding}">
Stanislav Kniazev
Thanks. That was the problem.
Vaccano
+1  A: 

Something odd here:

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

You're raising a property change event on a different property. Should probably be:

public string Value
{
    get { return _value; }
    set { _value = value; OnPropertyChanged("Value"); }
}
Aviad P.
Good point. I will fix that (that is the danger of copy and paste). However the issue was due to what Stanislav pointed out.
Vaccano