在WPF中另一个元素的作用域下如何设置x:Name属性
我需要创建几个弹出窗口。它们都具有相同的结构:在WPF中另一个元素的作用域下如何设置x:Name属性,wpf,nested,user-controls,contentcontrol,xname,Wpf,Nested,User Controls,Contentcontrol,Xname,我需要创建几个弹出窗口。它们都具有相同的结构: 左上部分 右上部分 内容部分 因此,我创建了一个usercontrol,在3个不同的位置保存一些内容。然后,我用dependencyProperties公开了这些“ContentControls” 我的问题是,添加到这些ContentControl中的任何内容都无法命名。。。不幸的是,我需要将一些内容从一个部分绑定到另一个部分的内容 (我可能做错了事情) 我发现了这个线索: 但没有一个解决方案对我有帮助:/因为这是一个老话题,我希望现在能解决它
- 左上部分
- 右上部分
- 内容部分
<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(""));
}
}
使用System.Windows;
使用System.Windows.Controls;
命名空间VirtualTrain.Atrac.Views.PopUps
{
///
///GenericPopUpView.xaml的交互逻辑
///
公共部分类GenericPopUpView:UserControl
{
公共GenericPopUpView()
{
初始化组件();
}
公共字符串标题
{
获取{return(string)GetValue(TitleProperty);}
set{SetValue(TitleProperty,value);}
}
公共新字体大小
{
获取{return(int)GetValue(FontSizeProperty);}
set{SetValue(FontSizeProperty,value);}
}
公共框架元素LeftButtonGroupElement
{
get{return(FrameworkElement)GetValue(LeftButtonGroupElementProperty);}
set{SetValue(LeftButtonGroupElementProperty,value);}
}
公共框架元素RightButtonGroupElement
{
get{return(FrameworkElement)GetValue(RightButtonGroupElementProperty);}
set{SetValue(RightButtonGroupElementProperty,value);}
}
公共框架元素ContentElement
{
get{return(FrameworkElement)GetValue(ContentElementProperty);}
set{SetValue(ContentElementProperty,value);}
}
//使用DependencyProperty作为Fontsize的后台存储。这将启用动画、样式、绑定等。。。
公共静态只读新从属属性FontSizeProperty=
DependencyProperty.Register(“FontSize”、typeof(int)、typeof(GenericPopUpView)、new PropertyMetadata(14));
//使用DependencyProperty作为RightButtonGroupElement的后台存储。这将启用动画、样式、绑定等。。。
公共静态只读DependencyProperty RightButtonGroupElementProperty=
Register(“RightButtonGroupElement”、typeof(FrameworkElement)、typeof(GenericPopUpView)、new PropertyMetadata(null));
//使用DependencyProperty作为LeftButtonGroupElement的后备存储。这将启用动画、样式、绑定等。。。
公共静态只读从属属性LeftButtonGroupElementProperty=
Register(“LeftButtonGroupElement”、typeof(FrameworkElement)、typeof(GenericPopUpView)、new PropertyMetadata(null));
//使用DependencyProperty作为MyProperty的备份存储。这将启用动画、样式设置、绑定等。。。
公共静态只读DependencyProperty ContentElementProperty=
Register(“ContentElement”、typeof(FrameworkElement)、typeof(GenericPopUpView)、newpropertymetadata(null));
//使用DependencyProperty作为标题的后备存储。这将启用动画、样式、绑定等。。。
公共静态只读从属属性TitleProperty=
DependencyProperty.Register(“标题”、typeof(字符串)、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>
下面是我得到的错误:
无法在元素“Label”上设置名称属性值“Test”。“Label”在元素“GenericPopUpView”的作用域下,该元素在另一个作用域中定义时已注册了名称。第36行位置11.VirtualTrain.Atrac…\VirtualTrain.Atrac\Views\PopUps\Start\UserControl1.xaml 36
为了访问此属性的值并能够从另一个“作用域”对其进行绑定,我做了一件事,就是使用ViewModel存储数据(绑定)。然后我将我的属性(从另一个作用域)绑定到此VM属性上
这不是我想要的方式(仅限xaml),但它可以工作。为什么需要为
标签指定一个名称?如何使用该名称?我必须在另一个控件的属性上设置一个绑定,该属性引用标签的一个属性