ali
ali

Reputation: 63

binding error in xaml

i have created an instance of a class in the code and bind one of the properties of that class named"Name" to a textbox but the binding does not work. can you help me? code behind:

    public partial class BlackOilWindow : FluidsWindowClass
    {
        public Classes.ModelInOutClasses.BlackOilFluidModel _FluidBlackOilClass;
        public BlackOilWindow(Classes.ModelInOutClasses.BlackOilFluidModel inputBlackOilClass):base(inputBlackOilClass)
        {
            if (inputBlackOilClass == null)
            {
                inputBlackOilClass = new ModelInOutClasses.BlackOilFluidModel();
            }
            _FluidBlackOilClass = inputBlackOilClass;
            InitializeComponent();



        }
   }

Xaml:

<TextBox 
    HorizontalAlignment="Left" 
    Height="23" 
    Margin="73,7,0,0" 
    TextWrapping="Wrap"
    VerticalAlignment="Top" 
    Width="120" 
    Text="{Binding ElementName=_FluidBlackOilClass, Path=Name, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
    />

Upvotes: 0

Views: 263

Answers (2)

Two problems. First, it has to be a property (and take that underscore off the front if it's public; by convention, the underscore prefix means "private field"):

public Classes.ModelInOutClasses.BlackOilFluidModel FluidBlackOilClass {
    get; set;
}

Second, by default, the Binding looks for the specified property on the DataContext object. As far as I can tell from what you're saying, the Window's DataContext is null. ElementName is used to refer to a control, a UI control, that has an x:Name property. FluidBlackOilClass is not that. And (trivia time!) Mode is unneeded in this case; the control's property has a default value for that, and TwoWay is the default for TextBox.Text.

So your binding can't work anyway, regardless of whether FluidBlackOilClass is a field or property. FluidBlackOilClass is a property of the window. Here's how to bind to a property of the window (or in this case, a property that belongs to a property of the window -- same difference, different Path):

<TextBox 
    HorizontalAlignment="Left" 
    Height="23" 
    Margin="73,7,0,0" 
    TextWrapping="Wrap"
    VerticalAlignment="Top" 
    Width="120" 
    Text="{Binding FluidBlackOilClass.Name, RelativeSource={RelativeSource AncestorType=Window}, UpdateSourceTrigger=PropertyChanged}"
    />    

You'd be better off with a proper viewmodel that implements INotifyPropertyChanged. You should not implement INotifyPropertyChanged on the Window as suggested in the accepted answer to this duplicate. That seems arbitrary at first, but you soon find that the separation makes your code much easier to deal with.

Upvotes: 1

Lupu Silviu
Lupu Silviu

Reputation: 1165

You used binding on a member of the class. Binding works only for Properties. A fast correction would be this:

 public Classes.ModelInOutClasses.BlackOilFluidModel _FluidBlackOilClass{get;set;}

Adapt the solution to your requirements.

Upvotes: 0

Related Questions