Wouterdek/NodeNetwork

Property binding to LeadingControlPresenterStyle

tbandzava opened this issue · 2 comments

Is it possible to bind a text property from a View Model to LeadingControlPresenterStyle, so it's always displayed. Example below does not display any text:

<UserControl x:Class="MyEditorApp.Views.SomeNodeView"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:local="clr-namespace:MyEditorApp.Views"
  xmlns:views="clr-namespace:NodeNetwork.Views;assembly=NodeNetwork"
  mc:Ignorable="d" 
  d:DesignHeight="300" d:DesignWidth="300">
  <Grid >
    <views:NodeView x:Name="SomeView" TitleFontSize="14"  ArrowSize="0" CornerRadius="5">
      <views:NodeView.LeadingControlPresenterStyle>
        <Style TargetType="ContentPresenter">
          <Setter Property="ContentTemplate">
            <Setter.Value>
              <DataTemplate DataType="ViewModels:SomeViewModel">
                <!-- TODO: NEED TO FIGURE OUT HOW TO BIND THE SomeTextProperty FROM THE VIEW MODEL -->
                <Label Foreground="White" Content="{Binding SomeTextProperty}" />
              </DataTemplate>
            </Setter.Value>
          </Setter>
        </Style>
      </views:NodeView.LeadingControlPresenterStyle>
    </views:NodeView>
  </Grid>
</UserControl>

where:

public partial class SomeNodeView : IViewFor<SomeViewModel>
{ ... }

and

public class SomeViewModel : NodeViewModel
{
    public string SomeTextProperty { get; set; } = "A text to display";
    ...
}

Something like this should work:

<views:NodeView.LeadingControlPresenterStyle>
    <Style TargetType="ContentPresenter">
        <Setter Property="Content" Value="{Binding }"/>
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate DataType="ViewModels:SomeViewModel">
                    <Label Foreground="White" Content="{Binding SomeTextProperty}" />
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</views:NodeView.LeadingControlPresenterStyle>

I've added line 3, which sets the Content of the ContentPresenter to the DataContext (= viewmodel) of the NodeView. This enables binding to the viewmodel properties in the datatemplate.

Worked beautifully. Thanks very much!