如何使用NAMED内容创建WPF UserControl

时间:2009-04-15 11:41:25

标签: c# wpf xaml user-controls controls

我有一组带有附加命令和逻辑的控件,它们以相同的方式不断重复使用。我决定创建一个包含所有常用控件和逻辑的用户控件。

但是我还需要控件才能保存可以命名的内容。我尝试了以下方法:

<UserControl.ContentTemplate>
    <DataTemplate>
        <Button>a reused button</Button>
        <ContentPresenter Content="{TemplateBinding Content}"/>
        <Button>a reused button</Button>
    </DataTemplate>
</UserControl.ContentTemplate>

但是,似乎无法命名放置在用户控件中的任何内容。例如,如果我按以下方式使用控件:

<lib:UserControl1>
     <Button Name="buttonName">content</Button>
</lib:UserControl1>

我收到以下错误:

  

无法设置名称属性值'buttonName'   在元素'按钮'上。 '按钮'是   在元素的范围内   'UserControl1',已经有了   在其中定义时注册的名称   另一个范围。

如果我删除了buttonName,那么它会编译,但是我需要能够命名内容。我怎样才能做到这一点?

10 个答案:

答案 0 :(得分:40)

答案是不使用UserControl来执行此操作。

创建一个扩展 ContentControl

的类
public class MyFunkyControl : ContentControl
{
    public static readonly DependencyProperty HeadingProperty =
        DependencyProperty.Register("Heading", typeof(string),
        typeof(HeadingContainer), new PropertyMetadata(HeadingChanged));

    private static void HeadingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((HeadingContainer) d).Heading = e.NewValue as string;
    }

    public string Heading { get; set; }
}

然后使用样式指定内容

<Style TargetType="control:MyFunkyControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="control:MyFunkyContainer">
                <Grid>
                    <ContentControl Content="{TemplateBinding Content}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

最后 - 使用它

<control:MyFunkyControl Heading="Some heading!">            
    <Label Name="WithAName">Some cool content</Label>
</control:MyFunkyControl>

答案 1 :(得分:17)

使用XAML时似乎无法做到这一点。当我实际拥有我需要的所有控件时,自定义控件似乎有点过分,但只需要将它们与一小部分逻辑组合在一起并允许命名内容。

mackenir提出的JD's blog解决方案似乎是最好的妥协。扩展JD的解决方案以允许仍然在XAML中定义控件的方法可以如下:

    protected override void OnInitialized(EventArgs e)
    {
        base.OnInitialized(e);

        var grid = new Grid();
        var content = new ContentPresenter
                          {
                              Content = Content
                          };

        var userControl = new UserControlDefinedInXAML();
        userControl.aStackPanel.Children.Add(content);

        grid.Children.Add(userControl);
        Content = grid;           
    }

在上面的示例中,我创建了一个名为UserControlDefinedInXAML的用户控件,它与使用XAML的任何普通用户控件一样定义。在我的UserControlDefinedInXAML中,我有一个名为aStackPanel的StackPanel,我希望在其中显示我的命名内容。

答案 2 :(得分:3)

我使用的另一个选择是在Name事件中设置Loaded属性。

在我的情况下,我有一个相当复杂的控件,我不想在代码隐藏中创建它,它寻找一个具有特定行为的特定名称的可选控件,因为我注意到我可以设置DataTemplate中的名字我认为我也可以在Loaded事件中执行此操作。

private void Button_Loaded(object sender, RoutedEventArgs e)
{
    Button b = sender as Button;
    b.Name = "buttonName";
}

答案 3 :(得分:3)

有时您可能只需要引用C#中的元素。根据用例,您可以设置x:Uid而不是x:Name,并通过调用像Get object by its Uid in WPF这样的Uid查找器方法来访问元素。

答案 4 :(得分:1)

您可以在用户控件中使用此帮助程序设置名称:

using System;
using System.Reflection;
using System.Windows;
using System.Windows.Media;
namespace UI.Helpers
{
    public class UserControlNameHelper
    {
        public static string GetName(DependencyObject d)
        {
            return (string)d.GetValue(UserControlNameHelper.NameProperty);
        }

        public static void SetName(DependencyObject d, string val)
        {
            d.SetValue(UserControlNameHelper.NameProperty, val);
        }

        public static readonly DependencyProperty NameProperty =
            DependencyProperty.RegisterAttached("Name",
                typeof(string),
                typeof(UserControlNameHelper),
                new FrameworkPropertyMetadata("",
                    FrameworkPropertyMetadataOptions.None,
                    (d, e) =>
                    {
                        if (!string.IsNullOrEmpty((string)e.NewValue))
                        {
                            string[] names = e.NewValue.ToString().Split(new char[] { ',' });

                            if (d is FrameworkElement)
                            {
                                ((FrameworkElement)d).Name = names[0];
                                Type t = Type.GetType(names[1]);
                                if (t == null)
                                    return;
                                var parent = FindVisualParent(d, t);
                                if (parent == null)
                                    return;
                                var p = parent.GetType().GetProperty(names[0], BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty);
                                p.SetValue(parent, d, null);
                            }
                        }
                    }));

        public static DependencyObject FindVisualParent(DependencyObject child, Type t)
        {
            // get parent item
            DependencyObject parentObject = VisualTreeHelper.GetParent(child);

            // we’ve reached the end of the tree
            if (parentObject == null)
            {
                var p = ((FrameworkElement)child).Parent;
                if (p == null)
                    return null;
                parentObject = p;
            }

            // check if the parent matches the type we’re looking for
            DependencyObject parent = parentObject.GetType() == t ? parentObject : null;
            if (parent != null)
            {
                return parent;
            }
            else
            {
                // use recursion to proceed with next level
                return FindVisualParent(parentObject, t);
            }
        }
    }
}

并且您的窗口或控制代码后面设置由Property控制:

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

    }

    public Button BtnOK { get; set; }
}

你的窗口xaml:

    <Window x:Class="user_Control_Name.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:test="clr-namespace:user_Control_Name"
            xmlns:helper="clr-namespace:UI.Helpers" x:Name="mainWindow"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <test:TestUserControl>
                <Button helper:UserControlNameHelper.Name="BtnOK,user_Control_Name.MainWindow"/>
            </test:TestUserControl>
            <TextBlock Text="{Binding ElementName=mainWindow,Path=BtnOK.Name}"/>
        </Grid>
    </Window>

UserControlNameHelper获取您的控件名称和类名称,以将Control设置为Property。

答案 5 :(得分:0)

我选择为每个我需要的元素创建一个额外的属性:

    public FrameworkElement First
    {
        get
        {
            if (Controls.Count > 0)
            {
                return Controls[0];
            }
            return null;
        }
    }

这使我能够访问XAML中的子元素:

<TextBlock Text="{Binding First.SelectedItem, ElementName=Taxcode}"/>

答案 6 :(得分:0)

<Popup>
    <TextBox Loaded="BlahTextBox_Loaded" />
</Popup>

代码背后:

public TextBox BlahTextBox { get; set; }
private void BlahTextBox_Loaded(object sender, RoutedEventArgs e)
{
    BlahTextBox = sender as TextBox;
}

真正的解决方案是让微软解决这个问题,以及其他所有破坏视觉树等的问题。假设说。

答案 7 :(得分:0)

另一种解决方法:将元素引用为 RelativeSource

答案 8 :(得分:0)

在将一堆命名控件放入。

时,使用TabControl时遇到了同样的问题

我的解决方法是使用一个控件模板,其中包含要在标签页中显示的所有控件。在模板内部,您可以使用Name属性,并且数据绑定到来自其他控件的命名控件的属性,至少在同一模板内。

作为TabItem控件的内容,使用一个简单的Control并相应地设置ControlTemplate:

<Control Template="{StaticResource MyControlTemplate}"/>

从后面的代码访问模板中的那些命名控件需要使用可视化树。

答案 9 :(得分:0)

我遇到了这个问题并找到了一种解决方法,可以让您使用 Xaml 设计自定义控件。它仍然有一些技巧,但它解决了我所有的问题而没有任何明显的妥协。

基本上,您按照通常使用 xaml 的方式执行所有操作,但您还在控件模板本身上包含了一些标头声明,并且 Base64 对该模板进行了编码,以便在代码构造函数中加载。此 Xaml 摘录中未显示,但我的完整 Xaml 使用的命名空间实际上是针对 XamlTemplates 而不是 Controls 命名空间的。这是故意的,因为“发布”构建将开发性调试引用从我的生产控件命名空间中移开。更多内容见下文。

<ControlTemplate TargetType="{x:Type TabControl}" 
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid x:Name="templateRoot" 
          ClipToBounds="True" 
          SnapsToDevicePixels="True" 
          Background="Transparent"
          KeyboardNavigation.TabNavigation="Local">
        <Grid.ColumnDefinitions>
            <ColumnDefinition x:Name="ColumnDefinition0"/>
            <ColumnDefinition x:Name="ColumnDefinition1" Width="0"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition x:Name="RowDefinition0" Height="Auto"/>
            <RowDefinition x:Name="RowDefinition1" Height="*"/>
        </Grid.RowDefinitions>
        <TabPanel x:Name="HeaderPanel"
                  Panel.ZIndex="1"                          
                  Margin="{Binding MarginHeaderPanel, RelativeSource={RelativeSource AncestorType=TabControl}}"
                  Background="{Binding Background, RelativeSource={RelativeSource AncestorType=TabControl}}"
                  IsItemsHost="True"                          
                  KeyboardNavigation.TabIndex="2"/>
        <Border x:Name="blankregion" Panel.ZIndex="1" Margin="0" Padding="0" 
                Background="{Binding Background, RelativeSource={RelativeSource AncestorType=TabControl}}">
            <ContentPresenter x:Name="blankpresenter"                                      
                              KeyboardNavigation.TabIndex="1"    
                              Content="{Binding TabBlankSpaceContent, RelativeSource={RelativeSource AncestorType=TabControl}}"                                          
                              ContentSource="TabBlankSpaceContent" 
                              SnapsToDevicePixels="True"/>
        </Border>

        <Grid x:Name="ContentPanel">
            <Border 
                BorderBrush="{Binding BorderBrush, RelativeSource={RelativeSource AncestorType=TabControl}}"
                BorderThickness="{Binding BorderThickness, RelativeSource={RelativeSource AncestorType=TabControl}}"                       
                Background="{Binding SelectedItem.Background, RelativeSource={RelativeSource AncestorType=TabControl}}"
                KeyboardNavigation.DirectionalNavigation="Contained" 
                KeyboardNavigation.TabNavigation="Local"                                           
                CornerRadius="{Binding BorderRadius, RelativeSource={RelativeSource AncestorType=TabControl}}"
                KeyboardNavigation.TabIndex="3">
                <ContentControl x:Name="PART_SelectedContentHost" 
                                ContentTemplate="{Binding SelectedContentTemplate, RelativeSource={RelativeSource AncestorType=TabControl}}"
                                Content="{Binding SelectedContent, RelativeSource={RelativeSource AncestorType=TabControl}}"
                                ContentStringFormat="{Binding SelectedContentStringFormat, RelativeSource={RelativeSource AncestorType=TabControl}}" 
                                Margin="{Binding Padding, RelativeSource={RelativeSource AncestorType=TabControl}}"
                                SnapsToDevicePixels="{Binding SnapsToDevicePixels, RelativeSource={RelativeSource AncestorType=TabControl}}"/>
            </Border>

        </Grid>
    </Grid>
    <ControlTemplate.Triggers>
        <!--Triggers were removed for clarity-->
    </ControlTemplate.Triggers>
</ControlTemplate>

我要指出,上面的 XAML 没有命名它派生自的控件,模板中的所有内容都使用相对查找来绑定其属性;甚至是定制的。

在 C# 方面,我使用了来自 Xaml 的 Base64 编码版本的控件模板和指令来调整控件的开发/发布版本。理论是我在开发空间中的控件不会遇到本主题所涉及的问题,但会给我一种测试/开发它们的方法。发布的 DLL 版本似乎运行得非常好,并且构建的控件确实具有很好的设计时支持,就像它们在调试/开发方面所做的一样。

#if DEBUG
namespace AgileBIM.Controls
{
    public class AgileTabControl : AgileBIM.XamlTemplates.AgileTabControlDesigner { }
}

namespace AgileBIM.XamlTemplates
#else
namespace AgileBIM.Controls
#endif
{
#if DEBUG    
    public partial class AgileTabControlDesigner : TabControl
#else
    public class AgileTabControl : TabControl
#endif
    {

        

#if DEBUG
        private static Type ThisControl = typeof(AgileTabControlDesigner);
#else
        private static Type ThisControl = typeof(AgileTabControl);
        private string Template64 = "Base64 encoded template removed for clarity"
#endif


#if DEBUG
        public AgileTabControlDesigner() { InitializeComponent(); }
#else
        public AgileTabControl()
        {
            string decoded = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(Template64));
            System.IO.StringReader sr = new System.IO.StringReader(decoded);
            System.Xml.XmlReader xr = System.Xml.XmlReader.Create(sr);
            ControlTemplate ct = (ControlTemplate)System.Windows.Markup.XamlReader.Load(xr);

            DefaultStyleKey = ThisControl;
            Template = ct;
        }
#endif

        public Thickness MarginHeaderPanel 
        {
            get { return (Thickness)GetValue(MarginHeaderPanelProperty); } 
            set { SetValue(MarginHeaderPanelProperty, value); } 
        }
        public static readonly DependencyProperty MarginHeaderPanelProperty =
            DependencyProperty.Register("MarginHeaderPanel", typeof(Thickness), ThisControl, new PropertyMetadata(new Thickness(0)));

        public CornerRadius BorderRadius 
        { 
            get { return (CornerRadius)GetValue(BorderRadiusProperty); } 
            set { SetValue(BorderRadiusProperty, value); }
        }
        public static readonly DependencyProperty BorderRadiusProperty =
            DependencyProperty.Register("BorderRadius", typeof(CornerRadius), ThisControl, new PropertyMetadata(new CornerRadius(0)));

        public object TabBlankSpaceContent 
        { 
            get { return (object)GetValue(TabBlankSpaceContentProperty); } 
            set { SetValue(TabBlankSpaceContentProperty, value); } 
        }
        public static readonly DependencyProperty TabBlankSpaceContentProperty =
            DependencyProperty.Register("TabBlankSpaceContent", typeof(object), ThisControl, new PropertyMetadata());
    }
}

在创建要在您的主应用程序中使用的“发布”控件 DLL 之前要记住的重要事情是使用其控件模板的最新和最好版本更新您的 base64 编码字符串。这是因为发布版本与原始 Xaml 完全分离,完全依赖于编码版本。

上述控件和其他类似控件可以在 GitHub 上找到。这是一个我正在制作的库,旨在“解锁”许多我想要设置样式的标准控件不会公开的东西。那并添加一些不存在的功能。例如,上面的 TabControl 有一个额外的内容属性,用于利用选项卡标题的“未使用”区域。

重要说明:

  • 使用此方法会丢失基本样式,但如果您的自定义控件样式使用 BasedOn="{StaticResource {x:Type TabControl}}" 机制,则可以恢复所有样式。
  • 我需要找时间研究这是否会导致任何值得注意的内存泄漏,以及我是否可以采取任何措施来解决它们,如果有人对此有任何想法,请在评论中告诉我。