WPF绑定问题
我的窗口中有一个带有工具提示的矩形, 单击该按钮本应更改工具提示文本,但事实并非如此。
XAML:
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.Resources>
<ToolTip x:Key="@tooltip">
<TextBlock Text="{Binding Name}" />
</ToolTip>
</Grid.Resources>
<Rectangle Width="200" Height="200" Fill="LightBlue" VerticalAlignment="Center" HorizontalAlignment="Center"
ToolTip="{DynamicResource @tooltip}" />
<Button Click="Button_Click" Grid.Row="1" Margin="20">Click Me</Button>
</Grid>
隐藏代码:
public partial class Window1 : Window
{
public Window1()
{
DataContext = new Person { Name = "A" };
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
DataContext = new Person { Name = "B" };
}
}
I have in my window a rectangle with a tooltip,
Clicking the button suppose to change the tooltip text but it doesn't.
XAML:
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.Resources>
<ToolTip x:Key="@tooltip">
<TextBlock Text="{Binding Name}" />
</ToolTip>
</Grid.Resources>
<Rectangle Width="200" Height="200" Fill="LightBlue" VerticalAlignment="Center" HorizontalAlignment="Center"
ToolTip="{DynamicResource @tooltip}" />
<Button Click="Button_Click" Grid.Row="1" Margin="20">Click Me</Button>
</Grid>
code behind:
public partial class Window1 : Window
{
public Window1()
{
DataContext = new Person { Name = "A" };
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
DataContext = new Person { Name = "B" };
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将绑定更改为:
示例:
原因:
此方法有效而您的无效的原因是
TextBlock
元素永远不会收到您绑定的名为Name
的属性发生更改的通知在你的代码中。为了解决这个问题,在此示例中,
TextBlock
绑定到Parent (ToolTip)
>PlacementTarget(按钮)
>DataContext(人)
>名称
属性。这里TextBlock
引用了PlacementTarget
,在您的情况下是一个Button
。Button
通知TextBlock
其DataContext
属性值发生更改,在该通知上,TextBlock
会更新自身。Change binding to:
Example:
Reason:
The reason this works and yours doesn't is because the
TextBlock
element never gets notified of a change in a property namedName
that you bind to in your code.To fix that, in this example,
TextBlock
is bound toParent (ToolTip)
>PlacementTarget (Button)
>DataContext (Person)
>Name
property. HereTextBlock
has reference toPlacementTarget
which in your case is aButton
.Button
notifiesTextBlock
of a change in it'sDataContext
property value on which notification,TextBlock
updates itself.是的,它有效,但并非始终如一。它有时有效,但并非总是有效。对我来说,这更像是一个计时错误。但是您可以稍微改变一下方法以使其正常工作。
首先,您必须在 Person 类中实现通知机制
示例
然后您需要为您的人员类创建一个属性并将其设置为您的数据上下文。现在您只需更改按钮单击时的名称值即可,效果很好
示例
看看这是否帮助...
Yes it works but not consistently.It works sometime but not all times.Seems more to be a timing bug to me.But you can change your approach a little to get it working
First you have to implement notification mechanism in your Person class
Sample
Then you need to Create a Property for your person class and set as your datacontext.Now you can just change the name value on your button click and it works well
Sample
See if this helps...