WPF 触发器/绑定不起作用
我正在尝试设置一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该属性 getter 返回自身,因此堆栈溢出。
并且设置器什么也没做,因此“无法工作”
可能是你想要的:
或者即使
如此,这仍然不会像你期望的那样工作,因为那里没有任何东西触发任何属性更改通知,所以没有人会注意到 myName 发生了变化。
that property getter is returning itself, hence the stack overflow.
and the setter is doing nothing, hence the "failing to work"
you probably want:
or even
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.