Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
Hello,

when I run following code in a window the binding just works normally, But when I do the same thing but make it a usercontrol it stops working.

I've tried so many things but I'm very very new to WPF and in need of help. (now the code differs between the usercontrol and the window because i'm trying every possible thing I know ;-) The Window.xaml displays the desired behaviour.

Please help me :-)


window.xaml
C#
<Window x:Class="testwpfgrid.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
    
    <StackPanel>
        <ListView Name="lv" ItemsSource="{Binding Path=AvosEntries}" IsSynchronizedWithCurrentItem="True">
            <ListView.View>
                <GridView AllowsColumnReorder="true" ColumnHeaderToolTip="Avos Information">
                    <GridViewColumn DisplayMemberBinding="{Binding Path=AvosName}" Header="Avos Name" Width="100"/>
                </GridView>
            </ListView.View>
        </ListView>
        <Button HorizontalAlignment="Right" Margin="5,5,5,5" Content="Add Row" Click="AddRow_Click" />
    </StackPanel>
</Window>

window.xaml.cs
C#
public partial class MainWindow : Window
   {
       public class AvosEntry
       {
           public string AvosName { get; set; }
       }
       public MainWindow()
       {
           AvosEntries = new ObservableCollection<AvosEntry>();
           fillList();
           InitializeComponent();
       }

       private void AddRow_Click(object sender, RoutedEventArgs e)
       {
           AvosEntries.Add(new AvosEntry
           {
               AvosName = "New Avos Entry"
           });
       }

       private void fillList()
       {
           for (int i = 1; i <= 10; i++)
           {
               AvosEntry avosEntry = new AvosEntry();
               avosEntry.AvosName = "Avos " + i;

               AvosEntries.Add(avosEntry);
           }
       }

       public ObservableCollection<AvosEntry> AvosEntries { get; private set; }
   }




usercontrol.xaml
C#
<UserControl x:Class="Genesyslab.Desktop.Modules.Extension.avos.MySample.MySampleView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <StackPanel>
        <ListView Name="lv" ItemsSource="{Binding Path=AvosEntries}" Foreground="#FFEF1212" removed="#FF868686" IsSynchronizedWithCurrentItem="True">
            <ListView.View>
                <GridView AllowsColumnReorder="true" ColumnHeaderToolTip="Avos Information">
                    <GridViewColumn DisplayMemberBinding="{Binding Path=AvosName}" Header="Avos Name" Width="100"/>
                </GridView>
            </ListView.View>
        </ListView>
        <Button HorizontalAlignment="Right" Margin="5,5,5,5" Content="Add Row" Click="AddRow_Click" />
    </StackPanel>
</UserControl>


usercontrol.xaml.cs
C#
public partial class MySampleView : UserControl
{
    public MySampleView()
    {
        AvosEntries = new ObservableCollection<AvosEntry>();
        fillList();
        InitializeComponent();
        Width = Double.NaN;
        Height = Double.NaN;
        lv.DataContext = AvosEntries;

    }

    private void AddRow_Click(object sender, RoutedEventArgs e)
    {
       AvosEntries.Add(new AvosEntry
        {
            AvosName = "New Avos Entry"
        });
    }


    private void fillList()
    {
        for (int i = 1; i <= 10; i++)
        {
            AvosEntry avosEntry = new AvosEntry();
            avosEntry.AvosName = "Avos " + i;

            AvosEntries.Add(avosEntry);
        }
    }

    public ObservableCollection<AvosEntry> AvosEntries
    {
        get { return (ObservableCollection<AvosEntry>)GetValue(AvosEntriesProperty); }
        set { SetValue(AvosEntriesProperty, value); }
    }

    public static DependencyProperty AvosEntriesProperty = DependencyProperty.Register("AvosEntries",
 typeof(ObservableCollection<AvosEntry>),
 typeof(MySampleView),
 new PropertyMetadata(new ObservableCollection<AvosEntry>()));
Posted
Comments
Matej Hlatky 29-Jan-13 10:18am    
Why do you set lv's DataContext to AvosEntries?
It should be “this” - instance of MySampleView (container control), like you did it in case of first sample code.
symbiosis320 29-Jan-13 10:57am    
Even with the code copied exactly from the window to the usercontrol it doesn't work :-)

symbiosis320 29-Jan-13 11:04am    
when i make the changes you suggest, I get a list but instead of binding the column to the AvosName property of the AvosEntry, I get the default object.ToString()...

please help!

The issue I see in you code is that you are trying to bind the ItemsSource to use the AvosEntries of your DataContext (which happens to be the collection pointed to by your AvosEntries property). Basically, you are trying to use the AvosEntries property of the AvosEntries collection.

I see two easy fixes:
* Change the DataContext to your instance of MySampleView. Which has an AvosEntries property.
* Or, change the binding in the ItemsSource to just "{Binding}". This will tell it to use the item in the DataContext.

As a side note, you might want to change the default value of AvosEntries property to null (unless you are trying to make some kind of pseudo-singleton collection).
 
Share this answer
 
try to name the UserControl and use ElementName within the binding right before the Path
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900