0

I have a model:

public class Table : ViewModelBase
{
    private int _id;
    private string _north;
    private string _east;
    private string _south;
    private string _west;

    public int Id
    {
        get
        {
            return _id;
        }
        set
        {
            _id = value; OnPropertyChanged();
        }
    }

    public string North
    {
        get { return _north; }
        set { _north = value; OnPropertyChanged();}
    }

    public string East
    {
        get { return _east; }
        set { _east = value; OnPropertyChanged();}
    }

    public string South
    {
        get { return _south; }
        set { _south = value; OnPropertyChanged();}
    }

    public string West
    {
        get { return _west; }
        set { _west = value; OnPropertyChanged();}
    }
}

Also ViewModel where tables list declared:

            Tables = new ObservableCollection<Table>();

And in XAML:

<ScrollViewer.Resources>
                <ItemsPanelTemplate x:Name="MyWrapPanel">
                    <toolkit:WrapPanel MinWidth="250" Width="{Binding ViewportWidth, ElementName=MyScrollViewer}" />
                </ItemsPanelTemplate>
            </ScrollViewer.Resources>

            <ItemsControl ItemsSource="{Binding Tables}" ItemsPanel="{StaticResource MyWrapPanel}" Grid.RowSpan="2" Grid.Row="1">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <controls:TableControl 
                            TableNumber="{Binding Id}"
                            North="{Binding North}"
                            East="{Binding East}"
                            South="{Binding South}"
                            West="{Binding West}"
                            />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

        </ScrollViewer>

But when I add elements to Tables list, there is all OK. But

TableNumber="{Binding Id}"
North="{Binding North}"
East="{Binding East}"
South="{Binding South}"
West="{Binding West}"

are not bound indeed.

rae1
  • 6,066
  • 4
  • 27
  • 48
alerya
  • 3,125
  • 3
  • 28
  • 50
  • What is `ViewModelBase`? You need to specify the property name in `OnPropertyChanged();`. Is that happening in `ViewModelBase` via reflection or something? – Arian Motamedi Sep 13 '13 at 19:32
  • post the code and XAML of the `TableControl`. – Federico Berasategui Sep 13 '13 at 19:32
  • @PoweredByOrange that is a [feature in .Net 4.5](http://blog.develop.com/inotifypropertychanged-in-net-45utm_campaigndevelopments-April-2012utm_sourcedevelopments/) – Federico Berasategui Sep 13 '13 at 19:34
  • @HighCore He didn't mention he's using .NET 4.5. These small mistakes are common for WPF beginners. – Arian Motamedi Sep 13 '13 at 19:35
  • @PoweredByOrange agreed – Federico Berasategui Sep 13 '13 at 19:36
  • 3
    Looks correct. Your issue may lay elsewhere. Make sure to turn up debug messages for databinding: http://i.stack.imgur.com/MF8i5.png Next, re-run and check the output window and see what errors are there. –  Sep 13 '13 at 19:36
  • Pls add the correct .Net version tag that you are using. – S2S2 Sep 13 '13 at 20:15
  • Its not necessary be 4.5 You can create your own attribute: [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] public sealed class CallerMemberNameAttribute : Attribute { } An it will be working nice ad neat even I Silverlight – alerya Sep 14 '13 at 05:42

1 Answers1

0

In the Xaml file of your user control named TableControl, add DataContext={Binding} in the root element and I think it should then work (if it's not present).

It could also be not working if you have DataContext set inside the code behind of your user control's (e.g.TableControl.xaml.cs) file to something like this.DataContext = this; which makes user control not use the DataContext of Tables collection.

You can use this SO Answer to check what exactly is in the DataContext of your user control TableControl.

Edit:

Try below also, because as per your comment, DataContext for TableControl is conflicting:

<DataTemplate>
    <controls:TableControl DataContext="{Binding}" 
        TableNumber="{Binding Id}"
        North="{Binding North}"
        East="{Binding East}"
        South="{Binding South}"
        West="{Binding West}"
        />
</DataTemplate>
Community
  • 1
  • 1
S2S2
  • 8,322
  • 5
  • 37
  • 65
  • The code had DataContext = this; and some DependenceProperites. Nothing interesting really at all :) – alerya Sep 14 '13 at 05:45
  • @alerya updated answer to add XAML as per your comment. Check if it works otherwise you would need to find some other alternative. – S2S2 Sep 14 '13 at 06:23