Wpf 单击时在用户控件内更改按钮内容
我意识到WPF对我来说仍然是一个魔法桶。问题似乎很简单。我有一个带按钮的用户控件。我想在单击时更改按钮内容文本 如果我在没有初始化按钮值的情况下使用用户控件打开表单,然后输入Wpf 单击时在用户控件内更改按钮内容,wpf,button,user-controls,Wpf,Button,User Controls,我意识到WPF对我来说仍然是一个魔法桶。问题似乎很简单。我有一个带按钮的用户控件。我想在单击时更改按钮内容文本 如果我在没有初始化按钮值的情况下使用用户控件打开表单,然后输入 void Button_Click(object sender, RoutedEventArgs e) { Button.Content = "New Value"; } 它起作用了 如果我在控件构造函数中通过button.Content=Init值动态初始化button值,则第二位button.Conte
void Button_Click(object sender, RoutedEventArgs e)
{
Button.Content = "New Value";
}
它起作用了
如果我在控件构造函数中通过button.Content=Init值动态初始化button值,则第二位button.Content=New值
它从未发生过,但按钮文本不再显示更改,至少看起来是这样
所以我决定用一个装订。MyUserControl+中声明的ButtonText属性与getter和setter对应的DependencyProperty,并尝试执行ButtonText=Init值;在构造函数和ButtonText中=新值;在按钮中单击。第一个有效,第二个仍然无效。我想是因为按钮点击中的数据上下文错误
在MyUserControl中,我尝试了一些方法,包括
<Button x:Name="Button"
Content="{Binding Path=ButtonText, RelativeSource={RelativeSource AncestorType=UserControl}}"
Click="Button_Click" />
<Button x:Name="Button"
Content="{Binding Path=ButtonText, Element=MyUserControl}"
Click="Button_Click" />
似乎什么都不管用
实现我需要的最简单的方法是什么,即动态初始化和动态更改?如果可能的话,请解释为什么我的第一个直接方法不起作用,以及绑定方法缺少什么
非常感谢
编辑:
另一种方法是使用触发器
此示例适用于:
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Content" Value="Init Value"/>
<Style.Triggers>
<Trigger Property="IsClicked" Value="True">
<Setter Property="Content" Value="New Value" />
</Trigger>
</Style.Triggers>
</Style>
我的问题是我需要一个数据触发器,就像这样
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Content" Value="Init Value"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=MyUserControlProperty, ElementName=MyUserControl}" Value="True">
<Setter Property="Content" Value="New Value" />
</DataTrigger>
</Style.Triggers>
</Style>
如何使MyUserControlProperty值更改正确传播
最有趣的是,如果我以新的形式打开控件,那么Button.Content=Init值的初始化不会把事情搞砸,一切正常。怎么回事?为什么这项简单的任务如此艰巨,为什么会有如此多的行为?当有绑定设置时,不要直接设置按钮内容。如果在构造函数中设置Button.Content,则可以有效地删除绑定。 这项工作:
<Window x:Class="StackOverflow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<Button Content="{Binding Path=ButtonText1, RelativeSource={RelativeSource AncestorType=Window}}" Click="Button1_OnClick"></Button>
</StackPanel>
</Window>
如果看不到用户控件的所有相关代码部分,就很难判断哪里出了问题。您可能混淆了UserControl中的元素名称。当您这样编写按钮时,通过单击处理程序更改按钮的内容总是有效的:Buttonsender.Content=New Value;这并不能回答问题。直接设置按钮的内容是完全有效的。当然是对的。我的第一个答案太简单了……你现在的答案显然也适用。然而,根据OP在他们的问题中所写的,这正是他们所尝试的。根据问题,当他们也建立了绑定时,他们没有直接设置内容。这只是两个不同的试验。正如我在对这个问题的评论中所指出的,如果没有看到更多细节,就无法回答。再次编辑。。。在任何情况下,操作设置按钮。内容直接在构造函数中。这就杀死了binding.OP。它清楚地写道,当没有绑定时,他们直接设置内容的尝试失败了。使用绑定时,它们不直接设置内容,而是设置源属性ButtonText=New Value;。你不应该拼命地回答一个目前无法回答的问题。
using System.Windows;
namespace StackOverflow
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
ButtonText1 = "Paul";
}
private void Button1_OnClick(object sender, RoutedEventArgs e)
{
ButtonText1 = "Maria";
}
public string ButtonText1
{
get => (string)GetValue(ButtonText1Property);
set => SetValue(ButtonText1Property, value);
}
// Using a DependencyProperty as the backing store for ButtonText1. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ButtonText1Property =
DependencyProperty.Register("ButtonText1", typeof(string), typeof(MainWindow), new PropertyMetadata("Peter"));
}
}