How to bind a variable with a textblock

…衆ロ難τιáo~ 提交于 2019-12-05 20:29:33

问题


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!


回答1:


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

}



回答2:


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;



回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/4344584/how-to-bind-a-variable-with-a-textblock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!