如何从xaml访问UserControl中的按钮? [英] How do I Access Buttons inside a UserControl from xaml?

查看:92
本文介绍了如何从xaml访问UserControl中的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在工作中,我有几个页面,每个页面的按钮位于相同的位置,并且具有相同的属性.每个页面也有细微的差别.为此,我们创建了一个userControl模板并将所有按钮放入其中,然后将该用户控件应用于所有页面.但是,现在很难访问按钮并从每个页面的xaml修改按钮,因为它们位于页面上的UserControl中..... 如何优雅地从每个页面访问按钮?

At work I have several pages, each with buttons in the same places, and with the same properties. Each page also has minor differences. To that end, we created a userControl Template and put all the buttons in it, then applied that user control to all the pages. However, now it's rather hard to access the buttons and modify them from each page's xaml, because they are inside a UserControl on the page..... How do I elegantly access the buttons from each page?

我尝试过的事情:

  1. 当前,我们绑定到一堆依赖项属性.我不喜欢此选项,因为我有很多按钮,并且需要控制这些按钮上的许多属性.结果就是成百上千的依赖属性,当我们需要更改某些东西时,真是一团糟.

  1. Currently, we bind to a bunch of dependency properties. I don't like this option because I have a lot of buttons, and need to control a lot of properties on those buttons. The result is hundreds of dependency properties, and a real mess to wade through when we need to change something.

另一种方法是使用样式.我通常喜欢这种方法,但是由于这些按钮位于另一个控件中,因此很难对其进行修改,并且模板一次只能恰好适合一个按钮.

Another method is to use styles. I like this method generally, but because these buttons are inside another control it becomes difficult to modify them, and the template would only be exactly right for one button, at one time.

亚当·坎普(Adam Kemp)发布了有关让用户只插入自己的按钮的信息这里,这是我目前正在尝试隐含/修改的方法.不幸的是,我无权使用Xamarin.

Adam Kemp posted about letting the user just insert their own button here, and this is the method I'm currently trying to impliment / modify. Unfortunately, I don't have access to Xamarin.

尽管代码运行时已插入模板 ,但模板并非正确更新了按钮.如果在MyButton Setter中设置一个断点,则可以看到 value 实际上是一个空按钮,而不是我在主窗口中分配的那个按钮.我该如何解决?

Although the template is inserted when the code runs, the template is not updating the button correctly. If I put a breakpoint in the MyButton Setter, I can see that value is actually an empty button, rather than the one I assigned in my main window. How do I fix this?

这是一些简化的代码:

我的模板UserControl的xaml:

My Template UserControl's xaml:

<UserControl x:Class="TemplateCode.Template"
     x:Name="TemplatePage"
     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"
     mc:Ignorable="d"
     d:DesignHeight="350"
     d:DesignWidth="525"
     DataContext="{Binding RelativeSource={RelativeSource Self}}"
     Background="DarkGray">

     <Grid>
          <Button x:Name="_button" Width="200" Height="100" Content="Template Button"/>
     </Grid>
</UserControl>

我的模板UserControl的代码后面:

My Template UserControl's Code Behind:

using System.Windows.Controls;

namespace TemplateCode
{
    public partial class Template : UserControl
    {
        public static Button DefaultButton;

        public Template()
        {
             InitializeComponent();
        }

        public Button MyButton
        {
            get
            {
                 return _button;
            }
            set
            {
                _button = value; //I get here, but value is a blank button?!

                // Eventually, I'd like to do something like:
                // Foreach (property in value) 
                // {
                //     If( value.property != DefaultButton.property) )
                //     {
                //         _button.property = value.property;
                //     }
                // }
                // This way users only have to update some of the properties
            }
        }
    }
}

现在是我要使用它的应用程序:

And now the application where I want to use it:

<Window x:Class="TemplateCode.MainWindow"
    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"
    mc:Ignorable="d"

    xmlns:templateCode="clr-namespace:TemplateCode"

    Title="MainWindow"
    Height="350"
    Width="525"
    Background="LimeGreen"
    DataContext="{Binding RelativeSource={RelativeSource Self}}" >

    <Grid>
        <templateCode:Template>
            <templateCode:Template.MyButton>

                <Button Background="Yellow" 
                    Content="Actual Button"
                    Width="200" 
                    Height="100"/>

            </templateCode:Template.MyButton>
        </templateCode:Template>
    </Grid>
</Window>

现在是其背后的代码:

Using System.Windows;
Namespace TemplateCode
{
    Public partial class MainWindow : Window
    {
        Public MainWindow()
        {
            InitializeComponent();
        }
    }
}

编辑:虽然我想删除 template userControl中不必要的依赖项属性,但我仍想在 button 上设置绑定XAML的属性.

While I want to remove unnecessary dependency properties in the template userControl, I'd still like to set bindings on the button's properties from the XAML.

推荐答案

基于@Funk答案的另一种选择是制作内容控件而不是模板上的按钮,然后将内容控件的内容绑定到代码中的ButtonProperty后面:

Another Option based on @Funk's answer is to make a content control instead of a button on the template, then bind the content control's content to your ButtonProperty in the code behind:

在模板上:

<ContentControl Content={Binding myButton} Width="200" Height="100"/>

在后面的模板代码中:

public static readonly DependencyProperty myButtonProperty =
        DependencyProperty.Register("Button", typeof(Button), typeof(Template),
            new UIPropertyMetadata(new PropertyChangedCallback(ButtonChangedCallback)));

,然后在主窗口上:

<Window.Resources>
    <Button x:Key="UserButton" 
            Background="Yellow" 
            Content="Actual Button"
            />
</Window.Resources>
<Grid>
    <templateCode:Template myButton="{StaticResource UserButton}"/>
</Grid>

对此的好处是Visual Studio足够聪明,可以在设计时显示此代码,并且总体上具有较少的代码.

The nice thing about this is that Visual Studio is smart enough to show this code at design time, as well as having less code overall.

您可以在内容控件或默认样式中为按钮设置固定的内容(例如位置,字体和颜色),然后仅修改按钮所需的部分.

You can set things constant things (like location, font, and coloring) for your button either on the content control or in a default style, and then modify just the parts you need for you button.

这篇关于如何从xaml访问UserControl中的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆