gpt4 book ai didi

c# - 如何从 xaml 访问 UserControl 中的按钮?

转载 作者:可可西里 更新时间:2023-11-01 08:45:57 27 4
gpt4 key购买 nike

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

我尝试过的:

  1. 目前,我们绑定(bind)了一堆依赖属性。我不喜欢这个选项,因为我有很多按钮,并且需要控制这些按钮的很多属性。其结果是数百个依赖属性,当我们需要更改某些内容时,要费力地处理一团糟。

  2. 另一种方法是使用样式。我通常喜欢这种方法,但是因为这些按钮在另一个控件中,所以很难修改它们,而且模板一次只能完全适合一个按钮。

  3. Adam Kemp 发表了关于让用户只需插入自己的按钮的帖子 here ,这是我目前正在尝试实现/修改的方法。不幸的是,我无法访问 Xamarin。

虽然代码运行时插入了模板,但模板没有正确更新按钮。如果我在 MyButton Setter 中放置一个断点,我可以看到 value 实际上是一个空按钮,而不是我在主窗口中分配的按钮。我该如何解决这个问题?

这里是一些简化的代码:

我的模板 UserControl 的 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 的代码隐藏:

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

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

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

编辑:虽然我想删除模板用户控件中不必要的依赖属性,但我仍然想在按钮上设置绑定(bind)来自 XAML 的属性。

最佳答案

与其使用许多依赖属性,不如使用样式方法。样式包含按钮控件可用的所有属性。

我会为 UserControl 中的每个按钮样式创建一个 DependencyProperty。

public partial class TemplateUserControl : UserControl
{
public TemplateUserControl()
{
InitializeComponent();
}

public static readonly DependencyProperty FirstButtonStyleProperty =
DependencyProperty.Register("FirstButtonStyle", typeof (Style), typeof (TemplateUserControl));

public Style FirstButtonStyle
{
get { return (Style)GetValue(FirstButtonStyleProperty); }
set { SetValue(FirstButtonStyleProperty, value); }
}

public static readonly DependencyProperty SecondButtonStyleProperty =
DependencyProperty.Register("SecondButtonStyle", typeof (Style), typeof (TemplateUserControl));

public Style SecondButtonStyle
{
get { return (Style)GetValue(SecondButtonStyleProperty); }
set { SetValue(SecondButtonStyleProperty, value); }
}
}

然后修改按钮的 xaml 以选择这些样式:

<UserControl x:Class="MyApp.TemplateUserControl"
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="200" d:DesignWidth="300"
Background="DarkGray">
<StackPanel>
<Button x:Name="_button" Width="200" Height="100"
Style="{Binding Path=FirstButtonStyle, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
<Button x:Name="_button2" Width="200" Height="100"
Style="{Binding Path=SecondButtonStyle, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
</StackPanel>
</UserControl>

现在当必须自定义按钮时,可以通过自定义样式来实现:

<StackPanel>
<StackPanel.Resources>
<!--common theme properties-->
<Style TargetType="Button" x:Key="TemplateButtonBase">
<Setter Property="FontSize" Value="18"/>
<Setter Property="Foreground" Value="Blue"/>
</Style>

<!--unique settings of the 1st button-->
<!--uses common base style-->
<Style TargetType="Button" x:Key="BFirst" BasedOn="{StaticResource TemplateButtonBase}">
<Setter Property="Content" Value="1st"/>
</Style>

<Style TargetType="Button" x:Key="BSecond" BasedOn="{StaticResource TemplateButtonBase}">
<Setter Property="Content" Value="2nd"/>
</Style>
</StackPanel.Resources>

<myApp:TemplateUserControl FirstButtonStyle="{StaticResource BFirst}"
SecondButtonStyle="{StaticResource BSecond}"/>
</StackPanel>

enter image description here

关于c# - 如何从 xaml 访问 UserControl 中的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39886903/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com