Search code examples
c#xamlbindinggridcontentpresenter

Is it possible to bind Content in Xaml To a Grid in code


I know this is bad practice but I have created a Grid in code and would like to bind this grid to my view.

So Far I have:

View:

            <ContentPresenter Content="{Binding CustomerTagsView}"/>

Code

    private Grid _customerTagsView;
    public Grid CustomerTagsView
    {
        get { return _customerTagsView; }
        set 
        { 
            _customerTagsView = value;
            OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("CustomerTagsView"));
        }
    }

But the Get method is never entered. What have I done wrong?


Solution

  • My bad.. The method works its just in my case the binding was incorrect...

    System.Windows.Data Error: 40 : BindingExpression path error: 'CustomerTagsView' property not found on 'object' ''' (HashCode=16802356)'. BindingExpression:Path=CustomerTagsView; DataItem='' (HashCode=16802356); target element is 'ContentPresenter' (Name=''); target property is 'Content' (type 'Object')

    An important lesson. Always check your output window before running to StackOverflow

    <UserControl.Resources>
        <DataTemplate x:Key="container">
            <Border>
                <ContentPresenter Content="{Binding}" />
            </Border>
        </DataTemplate>
    </UserControl.Resources>
    <ContentControl Content="{Binding newThing.CustomerTagsView}" ContentTemplate="{StaticResource container}"/>