如何将变量与文本块绑定

发布于 2024-10-06 11:35:47 字数 856 浏览 5 评论 0原文

我想知道如何将文本块绑定到 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 技术交流群。

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

发布评论

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

评论(4

不可一世的女人 2024-10-13 11:35:47

如果您还希望文本框在购物车类更改时自动更新,您的类必须实现 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");
        }
    }

}

If you further want the TextBoxes to update automatically when your cart class changes, your class must implement the INotifyPropertyChanged interface:

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");
        }
    }

}
另类 2024-10-13 11:35:47

绑定中的 ElementName 用于引用其他控件,而不是后面代码中的变量。要在后面的代码中引用变量,您需要将该变量分配给控件的 DataContext 属性。

将出现的以下代码行 : 替换

<TextBlock Name="Subtotal" FontFamily="Resources/#Charlemagne Std" FontSize="20" Text="{Binding ElementName=cart, Path=SubTotal}"></TextBlock>

为 :

<TextBlock Name="Subtotal" FontFamily="Resources/#Charlemagne Std" FontSize="20" Text="{Binding Path=SubTotal}"></TextBlock>

在窗口的构造函数或 Load 事件中,编写以下代码:

this.DataContext = cart;

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.

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 :

this.DataContext = cart;
我乃一代侩神 2024-10-13 11:35:47

两种解决方案。

第一个解决方案:

购物车作为数据源放在后面的代码中:

DataSource = cart;

并按如下方式绑定到它:

{Binding Path=PropertyOfCart}

第二个解决方案:

绑定到您的根控件具有 ElementName 绑定,并通过此控件上的属性获取购物车:

命名您的根/父控件,其中 cart 是属性:

<UserControl .....snip..... x:Name="Root">

像这样绑定到它:

{Binding ElementName=Root, Path=Cart.PropertyOfCart}

请注意,Cart 必须是您的 UserControl 的属性,并且不是一个字段

Two solutions..

First solution:

Put the cart as DataSource in your code behind:

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:

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

许你一世情深 2024-10-13 11:35:47

您需要将您的类设置为表单的数据源。另请参阅此问题

You need to set your class as the data source for your form. See also this question.

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