单击时在用户控件内更改按钮内容

时间:2019-05-27 23:41:41

标签: wpf button user-controls

我意识到WPF对我来说仍然是一大魔咒。问题似乎很简单。我有一个带有按钮的用户控件。我想更改单击时的按钮内容(文本)。

如果我使用用户控件打开表单而不初始化按钮值,然后在

中说
lambda x: x is not None

有效。

如果我通过void Button_Click(object sender, RoutedEventArgs e) { Button.Content = "New Value"; } 在控件构造函数中动态初始化按钮值,则第二位Button.Content = "Init Value" 永远不会发生(它会发生,但是按钮文本不会再显示该更改,至少不会显示该更改)。

因此,我决定使用绑定。在MyUserControl中声明了Button.Content = "New Value"属性(以及带有getter和setter的相应DependencyProperty),并尝试在构造函数中执行ButtonText,在ButtonText = "Init Value";中进行了ButtonText = "New Value";。第一个起作用,第二个仍然不起作用。我认为是因为Button_Click()中的数据上下文错误?

在MyUserControl中,我尝试了一些操作,包括

Button_Click()

似乎没有任何作用。

实现我所需的最简单的方法是什么,即动态初始化和动态更改?如果可能的话,请解释一下为什么我的第一个(直接)方法不起作用以及缺少什么绑定方法。

非常感谢!

编辑:

一种替代方法是使用触发器。

此示例有效:

<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" />

我的问题是我需要这样的DataTrigger

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

如何使MyUserControlProperty值更改正确传播?

最有趣的是,如果我以新形式打开控件,则<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> 的初始化不会使事情搞砸,一切都会正常进行。什么啊为什么这个简单的任务如此困难,为什么会有如此众多的行为?

1 个答案:

答案 0 :(得分:-1)

当您在其上设置了绑定时,请不要直接设置Button.Content。如果在构造函数中设置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>

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"));


    }
}
相关问题