如何将变量与文本块绑定
我想知道如何将文本块绑定到 C# 类中的变量。
基本上我的 .cs 文件中有一个“cart”变量。在该 Cart 类中,我可以访问不同的总计。
以下是我的绑定内容,但它似乎没有绑定到变量...
<StackPanel
Width="Auto"
Height="Auto"
Grid.ColumnSpan="2"
Grid.Row="5"
HorizontalAlignment="Right">
<TextBlock
Name="Subtotal"
FontFamily="Resources/#Charlemagne Std"
FontSize="20"
Text="{Binding ElementName=cart, Path=SubTotal}">
</TextBlock>
<TextBlock
Name="Tax"
FontFamily="Resources/#Charlemagne Std"
FontSize="20"
Text="{Binding ElementName=cart, Path=Tax}">
</TextBlock>
<TextBlock
Name="Total"
FontFamily="Resources/#Charlemagne Std"
FontSize="20"
Text="{Binding ElementName=cart, Path=Total}">
</TextBlock>
</StackPanel>
正确的方法是什么?再次感谢您的帮助!
I was wondering how I would be able to bind a text block to a variable within my C# class.
Basically I have a "cart" variable in my .cs file. Within that Cart class I have access to the different totals.
The following is what I have for binding, but it does not seem to bind to the variable...
<StackPanel
Width="Auto"
Height="Auto"
Grid.ColumnSpan="2"
Grid.Row="5"
HorizontalAlignment="Right">
<TextBlock
Name="Subtotal"
FontFamily="Resources/#Charlemagne Std"
FontSize="20"
Text="{Binding ElementName=cart, Path=SubTotal}">
</TextBlock>
<TextBlock
Name="Tax"
FontFamily="Resources/#Charlemagne Std"
FontSize="20"
Text="{Binding ElementName=cart, Path=Tax}">
</TextBlock>
<TextBlock
Name="Total"
FontFamily="Resources/#Charlemagne Std"
FontSize="20"
Text="{Binding ElementName=cart, Path=Total}">
</TextBlock>
</StackPanel>
What is the correct way of doing it? Thanks again for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您还希望文本框在购物车类更改时自动更新,您的类必须实现 INotifyPropertyChanged 接口:
If you further want the TextBoxes to update automatically when your cart class changes, your class must implement the
INotifyPropertyChanged
interface:绑定中的
ElementName
用于引用其他控件,而不是后面代码中的变量。要在后面的代码中引用变量,您需要将该变量分配给控件的 DataContext 属性。将出现的以下代码行 : 替换
为 :
在窗口的构造函数或 Load 事件中,编写以下代码:
ElementName
in binding is used to reference other controls, not variables in code behind. To reference variables in code behind, you need to assign that variable to a Control'sDataContext
property.Replace every occurrence of following line of code :
with :
And in your Window's constructor or Load event, write following code :
两种解决方案。
第一个解决方案:
将
购物车
作为数据源放在后面的代码中:并按如下方式绑定到它:
第二个解决方案:
绑定到您的根控件具有 ElementName 绑定,并通过此控件上的属性获取购物车:
命名您的根/父控件,其中 cart 是属性:
像这样绑定到它:
请注意,Cart 必须是您的 UserControl 的属性,并且不是一个字段
Two solutions..
First solution:
Put the
cart
as DataSource in your code behind:And bind to it as follows:
Second solution:
Bind to your root control with an ElementName binding, and get the cart through a property on this control:
Name your root/parent control where cart is a propery:
Bind to it like this:
Please note that Cart must be a property of your UserControl, and not a field
您需要将您的类设置为表单的数据源。另请参阅此问题。
You need to set your class as the data source for your form. See also this question.