WPF绑定问题

发布于 2024-10-09 00:51:12 字数 1006 浏览 1 评论 0原文

我的窗口中有一个带有工具提示的矩形, 单击该按钮本应更改工具提示文本,但事实并非如此。

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 技术交流群。

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

发布评论

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

评论(2

顾铮苏瑾 2024-10-16 00:51:12

将绑定更改为:

示例:

<ToolTip x:Key="@tooltip">
    <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Parent.PlacementTarget.DataContext.Name}" />
</ToolTip>

原因:

此方法有效而您的无效的原因是 TextBlock 元素永远不会收到您绑定的名为 Name 的属性发生更改的通知在你的代码中。

为了解决这个问题,在此示例中,TextBlock 绑定到 Parent (ToolTip) > PlacementTarget(按钮)> DataContext(人)> 名称 属性。这里 TextBlock 引用了 PlacementTarget,在您的情况下是一个 ButtonButton 通知 TextBlockDataContext 属性值发生更改,在该通知上,TextBlock 会更新自身。

Change binding to:

Example:

<ToolTip x:Key="@tooltip">
    <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Parent.PlacementTarget.DataContext.Name}" />
</ToolTip>

Reason:

The reason this works and yours doesn't is because the TextBlock element never gets notified of a change in a property named Name that you bind to in your code.

To fix that, in this example, TextBlock is bound to Parent (ToolTip) > PlacementTarget (Button) > DataContext (Person) > Name property. Here TextBlock has reference to PlacementTarget which in your case is a Button. Button notifies TextBlock of a change in it's DataContext property value on which notification, TextBlock updates itself.

惯饮孤独 2024-10-16 00:51:12

是的,它有效,但并非始终如一。它有时有效,但并非总是有效。对我来说,这更像是一个计时错误。但是您可以稍微改变一下方法以使其正常工作。

首先,您必须在 Person 类中实现通知机制

示例

 public class Person:INotifyPropertyChanged
{
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; OnPropertyChanged("Name"); }
    }



    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
    #endregion
}

然后您需要为您的人员类创建一个属性并将其设置为您的数据上下文。现在您只需更改按钮单击时的名称值即可,效果很好

示例

public Window1()
    {
        MyPerson = new Person();
        MyPerson.Name = "A";
        DataContext = MyPerson;
        InitializeComponent();
    }

    private Person myPerson;

    public Person MyPerson
    {
        get { return myPerson; }
        set { myPerson = value; OnPropertyChanged("MyPerson"); }
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MyPerson.Name = "B";
    }

看看这是否帮助...

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

 public class Person:INotifyPropertyChanged
{
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; OnPropertyChanged("Name"); }
    }



    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
    #endregion
}

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

public Window1()
    {
        MyPerson = new Person();
        MyPerson.Name = "A";
        DataContext = MyPerson;
        InitializeComponent();
    }

    private Person myPerson;

    public Person MyPerson
    {
        get { return myPerson; }
        set { myPerson = value; OnPropertyChanged("MyPerson"); }
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MyPerson.Name = "B";
    }

See if this helps...

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