-1

I need to create several popups. All of them share the same structure :

  • Top left section
  • Top Right section
  • Content section

Therefore, I created a usercontrol to hold some content at 3 different places. I then exposed these "ContentControls" with dependencyProperties.

My problem is that any content added to these ContentControls cannot be given a name... Unfortunately, I need want to bind some content from one section to content of another...

(I may be doing things wrong ^^")

I found this thread : How to create a WPF UserControl with NAMED content But none of the solutions helped me :/ Since it's an old topic, i hope it can now be solved...

Here's my usercontrol :

<UserControl
  x:Class="VirtualTrain.Atrac.Views.PopUps.GenericPopUpView"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:local="clr-namespace:VirtualTrain.Atrac.Views.PopUps"
  mc:Ignorable="d">
  <UserControl.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary
          Source="/VirtualTrain.Atrac;component/Resources/Style/Style.xaml" />
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </UserControl.Resources>
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition
        Height="37*" />
      <RowDefinition
        Height="155*" />
    </Grid.RowDefinitions>

    <Grid
      Background="{StaticResource HeaderBackgroundColor}">
      <Grid.ColumnDefinitions>
        <ColumnDefinition
          Width="auto" />
        <ColumnDefinition
          Width="*" />
        <ColumnDefinition
          Width="auto" />
      </Grid.ColumnDefinitions>

      <ContentControl
        x:Name="LeftContentElementControl"
        Grid.Column="0"
        Grid.Row="0"
        Content="{Binding Path=LeftButtonGroupElement ,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:GenericPopUpView}}}"/>

      <Label
        Grid.Column="1"
        Grid.Row="0"
        Style="{StaticResource GenericTitleLabel}"
        FontSize="{Binding Path=FontSize, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:GenericPopUpView}}}"
        Content="{Binding Path=Title, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:GenericPopUpView}}}" />

      <ContentControl
        x:Name="RightContentElementControl"
        Grid.Column="2"
        Grid.Row="0"
        Content="{Binding Path=RightButtonGroupElement ,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:GenericPopUpView}}}"/>
    </Grid>

    <Border
      Grid.Row="1"
      Grid.Column="0"
      Background="{StaticResource ContentBackgroundColor}">
      <ContentControl
        x:Name="ContentElementControl"
        Content="{Binding Path=ContentElement ,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:GenericPopUpView}}}" />
    </Border>
  </Grid>
</UserControl>

Here's my code behind :

using System.Windows;
using System.Windows.Controls;

namespace VirtualTrain.Atrac.Views.PopUps
{
  /// <summary>
  /// Interaction logic for GenericPopUpView.xaml
  /// </summary>
  public partial class GenericPopUpView : UserControl
  {
    public GenericPopUpView()
    {
      InitializeComponent();
    }




    public string Title
    {
      get { return (string)GetValue(TitleProperty); }
      set { SetValue(TitleProperty, value); }
    }

    public new int FontSize
    {
      get { return (int)GetValue(FontSizeProperty); }
      set { SetValue(FontSizeProperty, value); }
    }

    public FrameworkElement LeftButtonGroupElement
    {
      get { return (FrameworkElement)GetValue(LeftButtonGroupElementProperty); }
      set { SetValue(LeftButtonGroupElementProperty, value); }
    }

    public FrameworkElement RightButtonGroupElement
    {
      get { return (FrameworkElement)GetValue(RightButtonGroupElementProperty); }
      set { SetValue(RightButtonGroupElementProperty, value); }
    }

    public FrameworkElement ContentElement
    {
      get { return (FrameworkElement)GetValue(ContentElementProperty); }
      set { SetValue(ContentElementProperty, value); }
    }



    // Using a DependencyProperty as the backing store for Fontsize.  This enables animation, styling, binding, etc...
    public static readonly new DependencyProperty FontSizeProperty =
        DependencyProperty.Register("FontSize", typeof(int), typeof(GenericPopUpView), new PropertyMetadata(14));


    // Using a DependencyProperty as the backing store for RightButtonGroupElement.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty RightButtonGroupElementProperty =
        DependencyProperty.Register("RightButtonGroupElement", typeof(FrameworkElement), typeof(GenericPopUpView), new PropertyMetadata(null));



    // Using a DependencyProperty as the backing store for LeftButtonGroupElement.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty LeftButtonGroupElementProperty =
        DependencyProperty.Register("LeftButtonGroupElement", typeof(FrameworkElement), typeof(GenericPopUpView), new PropertyMetadata(null));


    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ContentElementProperty =
        DependencyProperty.Register("ContentElement", typeof(FrameworkElement), typeof(GenericPopUpView), new PropertyMetadata(null));



    // Using a DependencyProperty as the backing store for Title.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.Register("Title", typeof(string), typeof(GenericPopUpView), new PropertyMetadata(""));



  }
}

And here's an example of what i'd like to do :

<UserControl x:Class="VirtualTrain.Atrac.Views.PopUps.Start.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:popUps="clr-namespace:VirtualTrain.Atrac.Views.PopUps"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
  <popUps:GenericPopUpView
    Title="SomePopUp"
    FontSize="20">
    <!--PART LEFT-->
    <popUps:GenericPopUpView.LeftButtonGroupElement>
      <Grid>
        <Label Margin="10"
               Content="TEST"
               Foreground="White"/>
      </Grid>
    </popUps:GenericPopUpView.LeftButtonGroupElement>

    <!--PART RIGHT-->
    <popUps:GenericPopUpView.RightButtonGroupElement>
      <Grid>
        <Label
          Margin="10"
          Content="TEST"
          Foreground="White" />
      </Grid>
    </popUps:GenericPopUpView.RightButtonGroupElement>

    <!--PART CONTENT-->
    <popUps:GenericPopUpView.ContentElement>
      <Grid>
        <Label
          x:Name="Test"
          Margin="10"
          Content="TEST"
          Foreground="White" />
      </Grid>
    </popUps:GenericPopUpView.ContentElement>

  </popUps:GenericPopUpView>
</UserControl>

Here's the error I get : Cannot set Name attribute value 'Test' on element 'Label'. 'Label' is under the scope of element 'GenericPopUpView', which already had a name registered when it was defined in another scope. Line 36 Position 11. VirtualTrain.Atrac ...\VirtualTrain.Atrac\Views\PopUps\Start\UserControl1.xaml 36

Axel Samyn
  • 160
  • 1
  • 12

1 Answers1

0

One thing I did to access this property's value and be able to bind on it from antoher "scope" was to use a ViewModel to store the data (Binding). I then bound my property (from another scope) on this VM property...

Not really the way I wanted (xaml only) but it works.

Axel Samyn
  • 160
  • 1
  • 12