Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows WPF User-controls , +
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 Wink | ;-) The Window.xaml displays the desired behaviour.
 
Please help me Smile | :)
 

window.xaml
<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
 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
<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
	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 29-Jan-13 3:12am
Comments
Matej Hlatky at 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 at 29-Jan-13 10:57am
   
Even with the code copied exactly from the window to the usercontrol it doesn't work :-)
 
symbiosis320 at 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!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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).
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

try to name the UserControl and use ElementName within the binding right before the Path
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 269
1 OriginalGriff 240
2 CPallini 195
3 Maciej Los 177
4 BillWoodruff 173
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 11 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100