-1

我需要创建几个弹出窗口。它们都具有相同的结构:

  • 左上部分
  • 右上角部分
  • 内容部分

因此,我创建了一个用户控件来在 3 个不同的地方保存一些内容。然后我用dependencyProperties 暴露了这些“ContentControls”。

我的问题是添加到这些 ContentControls 的任何内容都不能命名...不幸的是,我需要将一些内容从一个部分绑定到另一个部分的内容...

(我可能做错了^^")

我找到了这个线程:How to create a WPF UserControl with NAMED content 但是没有一个解决方案对我有帮助:/因为这是一个老话题,我希望现在可以解决......

这是我的用户控件:

<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>

这是我背后的代码:

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(""));



  }
}

这是我想做的一个例子:

<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>

这是我得到的错误: 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

4

1 回答 1

0

我为访问此属性的值并能够从另一个“范围”绑定它所做的一件事是使用 ViewModel 来存储数据(绑定)。然后我将我的属性(来自另一个范围)绑定在这个 VM 属性上......

不是我想要的方式(仅限 xaml),但它有效。

于 2019-10-10T07:14:07.520 回答