WPF 触发器/绑定不起作用

发布于 2024-11-17 17:33:54 字数 2169 浏览 1 评论 0原文

我正在尝试设置一个 TextBox 子类,该子类将根据一些不同的事情更改其样式,但我遇到了两个问题。第一个触发器(VisualBrush 触发器)正确触发,但不会在字符串 myName 中写入文本。我尝试将 myName 设置为属性,但由于某种原因,set 方法抛出 StackOverFlowException。

第二个问题是 DataTrigger,即使 isRequired 设置为 false,它也不会被触发。

这一切都在继承 TextBox 的自定义控件内。

这是我的 XAML:

    <TextBox.Style>
    <Style TargetType="TextBox">
        <Style.Triggers>
            <Trigger Property="Text" Value="">
                <Setter Property="Background">
                    <Setter.Value>
                        <VisualBrush Stretch="None">
                            <VisualBrush.Visual>
                                <TextBlock Foreground="Gray" FontSize="24">
                                        <TextBlock.Text>
                                            <Binding Path="myName" RelativeSource="{RelativeSource Self}" />
                                        </TextBlock.Text>
                                </TextBlock>
                            </VisualBrush.Visual>
                        </VisualBrush>
                    </Setter.Value>
                </Setter>
            </Trigger>
            <DataTrigger Binding="{Binding Path=isRequired, Source={RelativeSource Self}}" Value="False">
                <Setter Property="Text" Value="100" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</TextBox.Style>

CS:

    public partial class SuperTB : TextBox
{
    public String myName
    {
        get { return myName; }
        set {}
    }

    DependencyProperty isRequiredProperty = DependencyProperty.Register("isRequired", typeof(Boolean), typeof(SuperTB));

    public Boolean isRequired
    {
        get { return (Boolean)GetValue(isRequiredProperty); }
        set { SetValue(isRequiredProperty, value); }
    }

    public SuperTB()
    {
        InitializeComponent();
        myName = "Unicorns!";
    }

}

这是 StackOverflows 的代码。也无法工作但没有例外的是:

public string myName = "Rainbows!";

I'm trying to setup a TextBox subclass that will change its style based on a few different things, and I'm running into two problems. The first Trigger, the VisualBrush one, triggers properly but won't write the text in the String myName. I tried making myName a property but for some reason the set method throws a StackOverFlowException.

The second problem is with the DataTrigger, which isn't getting triggered even though isRequired is set to false.

This is all within a custom control that inherits TextBox.

Here's my XAML:

    <TextBox.Style>
    <Style TargetType="TextBox">
        <Style.Triggers>
            <Trigger Property="Text" Value="">
                <Setter Property="Background">
                    <Setter.Value>
                        <VisualBrush Stretch="None">
                            <VisualBrush.Visual>
                                <TextBlock Foreground="Gray" FontSize="24">
                                        <TextBlock.Text>
                                            <Binding Path="myName" RelativeSource="{RelativeSource Self}" />
                                        </TextBlock.Text>
                                </TextBlock>
                            </VisualBrush.Visual>
                        </VisualBrush>
                    </Setter.Value>
                </Setter>
            </Trigger>
            <DataTrigger Binding="{Binding Path=isRequired, Source={RelativeSource Self}}" Value="False">
                <Setter Property="Text" Value="100" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</TextBox.Style>

CS:

    public partial class SuperTB : TextBox
{
    public String myName
    {
        get { return myName; }
        set {}
    }

    DependencyProperty isRequiredProperty = DependencyProperty.Register("isRequired", typeof(Boolean), typeof(SuperTB));

    public Boolean isRequired
    {
        get { return (Boolean)GetValue(isRequiredProperty); }
        set { SetValue(isRequiredProperty, value); }
    }

    public SuperTB()
    {
        InitializeComponent();
        myName = "Unicorns!";
    }

}

This is the code that StackOverflows it. Also failing to work but no Exception is:

public string myName = "Rainbows!";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蛮可爱 2024-11-24 17:33:54
 public string myName    
 {        
     get { return myName; }        
     set {}    
 }

该属性 getter 返回自身,因此堆栈溢出。

并且设置器什么也没做,因此“无法工作”

可能是你想要的:

 private string myName; // lower case!
 public string MyName    // upper case!
 {        
     get { return myName; }        
     set { myName = value; }    
 }

或者即使

 public string myName { get; set; }

如此,这仍然不会像你期望的那样工作,因为那里没有任何东西触发任何属性更改通知,所以没有人会注意到 myName 发生了变化。

 public string myName    
 {        
     get { return myName; }        
     set {}    
 }

that property getter is returning itself, hence the stack overflow.

and the setter is doing nothing, hence the "failing to work"

you probably want:

 private string myName; // lower case!
 public string MyName    // upper case!
 {        
     get { return myName; }        
     set { myName = value; }    
 }

or even

 public string myName { get; set; }

and even then this still won't work like you expect, since nothing is firing any property change notifications there, so nobody will notice that myName ever changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文