简体   繁体   English

WPF XAML绑定

[英]WPF XAML Binding

Hello I have a problem with a binding that I want to do and can't find any information on how to do it. 您好,我有一个我想做的绑定问题,找不到有关如何做的任何信息。 Basically I want to bind an object to a property of another object. 基本上我想将一个对象绑定到另一个对象的属性。 For example 例如

<TextBox Text="test" Tag="{Binding ElementName=TxtBx2}" x:Name="TxtBx1"/>
<TextBox Text="test" x:Name="TxtBx2"/>

This is kind of weird but it would help on the code that i'm implementing, so on a property of one object I want to have another object bind in xaml. 这有点怪异,但对我正在实现的代码有帮助,所以在一个对象的属性上,我想在xaml中绑定另一个对象。

I don't know if this is possible, any pointers would be helpful Thanks, Ruben 我不知道这是否可行,任何指针都会有所帮助,谢谢,鲁本

That is how you do it; 那就是你的做法; you just need to specify the Path . 您只需要指定Path

<TextBox Text="test" Tag="{Binding ElementName=TxtBx2, Path=Text}" x:Name="TxtBx1"/>
<TextBox Text="test" x:Name="TxtBx2"/>

If you are wanting the DataContext of the TextBox ; 如果您想要TextBoxDataContext then your Path would change accordingly. 那么您的路径就会相应更改。

<TextBox Text="test" Tag="{Binding ElementName=TxtBx2, Path=DataContext}" x:Name="TxtBx1"/>
<TextBox Text="test" x:Name="TxtBx2"/>

If you are needing to use the Tag property within a WPF application you might want to re-evaluate your approach as I have yet to use the Tag property since moving from WinForms as that need has been replaced by leveraging the data binding functionality within WPF. 如果您需要在WPF应用程序中使用Tag属性,则可能要重新评估您的方法,因为我尚未使用Tag属性,因为从WinForms迁移过来已被WPF中的数据绑定功能所取代。

UPDATE: 更新:

If your goal is to bind to a given control versus a property on the control; 如果您的目标是绑定到给定的控件而不是控件的属性; then don't specify the property name within the Path . 然后不要在Path指定属性名称。

Based on your goal; 根据您的目标; attached behaviors would be a better approach and allow you to wrap the functionality within the extended DataGrid . 附加的行为将是更好的方法,并允许您将功能包装在扩展的DataGrid

<TextBox Text="test" Tag="{Binding ElementName=TxtBx2,Path=Text}" x:Name="TxtBx1"/>
<TextBox Text="test" x:Name="TxtBx2"/>

Assuming you want the value of the Text property of TxtBx1 to be the value of the Text property in TxtBx2, you would use: 假设您希望TxtBx1的Text属性的值是TxtBx2中Text属性的值,则可以使用:

<TextBox x:Name="TxtBx1" Text="{Binding ElementName=TxtBx2, Path=Text}" />
<TextBox x:Name="TxtBx2" Text="test" />

Update 更新资料

Assuming (possibly incorrectly again!) that you want to bind the TxtBx1 element to the Tag of TxtBx2, you would use: 假设(可能再次错误!)您想将TxtBx1元素绑定到TxtBx2的标签,可以使用:

<TextBox x:Name="TxtBx1" Text="test" />
<TextBox x:Name="TxtBx2" Tag="{Binding ElementName=TxtBx1}" Text="test" />

Just out of interest, why do you want to do such a thing? 只是出于兴趣,您为什么要这样做?

Update 2 更新2

Assuming that you have a Datagrid that you've extended from the wpftoolkit datagrid and a user control that is a pager for that Datagrid, and when you move to another page you need to do some processing on the datagrid, then why don't you just either update the datagrid in your page change event (if using code behind), or update the items that the datagrid is bound to in your page change verb on your view model (if using MVVM)? 假定您有一个从wpftoolkit数据网格扩展的数据网格,并且是作为该数据网格的分页器的用户控件,并且当您移至另一个页面时,您需要对该数据网格进行一些处理,那为什么不呢?只是在页面更改事件中更新datagrid(如果使用后面的代码),还是在视图模型的页面更改动词中更新datagrid绑定的项(如果使用MVVM)?

Well, it does make sense to bind to an entire object(not to any specific property) with items control such as this: 好吧,使用诸如此类的项目控件将其绑定到整个对象(而不是任何特定的属性)确实很有意义:

<ListBox x:Name="pictureBox" 
ItemsSource=”{Binding Source={StaticResource photos}}" …> 
......
</ListBox>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM