wpf 如何将变量与文本块绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4344584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to bind a variable with a textblock
提问by Aero Chocolate
I was wondering how I would be able to bind a text block to a variable within my C# class.
我想知道如何将文本块绑定到我的 C# 类中的变量。
Basically I have a "cart" variable in my .cs file. Within that Cart class I have access to the different totals.
基本上我的 .cs 文件中有一个“cart”变量。在那个 Cart 类中,我可以访问不同的总数。
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!
正确的做法是什么?再次感谢您的帮助!
回答by Michael Hilus
If you further want the TextBoxes to update automatically when your cart class changes, your class must implement the INotifyPropertyChanged
interface:
如果您进一步希望 TextBoxes 在您的购物车类更改时自动更新,您的类必须实现该INotifyPropertyChanged
接口:
class Cart : INotifyPropertyChanged
{
// property changed event
public event PropertyChangedEventHandler PropertyChanged;
private int _subTotal;
private int _total;
private int _tax;
private void OnPropertyChanged(String property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
public int SubTotal
{
get
{
return _subTotal;
}
set
{
_subTotal = value;
OnPropertyChanged("SubTotal");
}
}
public int Total
{
get
{
return _total;
}
set
{
_total = value;
OnPropertyChanged("Total");
}
}
public int Tax
{
get
{
return _tax;
}
set
{
_tax = value;
OnPropertyChanged("Tax");
}
}
}
回答by decyclone
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's DataContext
property.
ElementName
in binding 用于引用其他控件,而不是后面代码中的变量。要在后面的代码中引用变量,您需要将该变量分配给控件的DataContext
属性。
Replace every occurrence of following line of code :
替换每次出现的以下代码行:
<TextBlock Name="Subtotal" FontFamily="Resources/#Charlemagne Std" FontSize="20" Text="{Binding ElementName=cart, Path=SubTotal}"></TextBlock>
with :
和 :
<TextBlock Name="Subtotal" FontFamily="Resources/#Charlemagne Std" FontSize="20" Text="{Binding Path=SubTotal}"></TextBlock>
And in your Window's constructor or Load event, write following code :
并在您的 Window 的构造函数或 Load 事件中,编写以下代码:
this.DataContext = cart;
回答by Arcturus
Two solutions..
两种解决方案..
First solution:
第一个解决方案:
Put the cart
as DataSource in your code behind:
将cart
as DataSource 放在您的代码后面:
DataSource = cart;
And bind to it as follows:
并按如下方式绑定到它:
{Binding Path=PropertyOfCart}
Second solution:
第二种解决方案:
Bind to your root control with an ElementName binding, and get the cart through a property on this control:
使用 ElementName 绑定绑定到您的根控件,并通过此控件上的属性获取购物车:
Name your root/parent control where cart is a propery:
命名您的根/父控件,其中购物车是一个属性:
<UserControl .....snip..... x:Name="Root">
Bind to it like this:
像这样绑定到它:
{Binding ElementName=Root, Path=Cart.PropertyOfCart}
Please note that Cart must be a property of your UserControl, and not a field
请注意, Cart 必须是您的 UserControl 的属性,而不是字段
回答by Thorsten Dittmar
You need to set your class as the data source for your form. See also this question.
您需要将您的类设置为表单的数据源。另请参阅此问题。