-1

Binding a textbox with value of Name2 doesn't work. Code is correct and works in a simple WPF application. Is there any other way to bind in devexpress?

enter image description here

<TextBox Height="20" TextWrapping="Wrap" Text="{Binding Path=Name2}" VerticalAlignment="Top" Margin="429,27,159,0" AcceptsReturn="True">
public partial class EntitiesView : UserControl, INotifyPropertyChanged
{
    private string _name2;
    public string Name2
    {
        get { return _name2; }
        set
        {
            _name2 = value;
            RaisePropertyChanged("Name2");
        }
    }

    public EntitiesView()
    {
        Name2 = "abcdefffffffffffff";
        DataContext = this;
        InitializeComponent();
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void RaisePropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
J. Steen
  • 15,470
  • 15
  • 56
  • 63
Dua Ali
  • 3,163
  • 3
  • 19
  • 13
  • Do you see any binding errors in the Debug Output window of Visual Studio? – slfan Jul 21 '13 at 11:07
  • Everything looks allright to me. But what do you need the TextBox.BindingGroup for? Is it possible that the bindings are cleared somehow? – slfan Jul 21 '13 at 16:46
  • Hi @slfan! Actually i am using Devexpress MVVM, in which we can only pass or show values via binding. That's why i need it. Code is working fine in wpf simple application but with this MVVM, there is something wrong. – Dua Ali Jul 21 '13 at 16:52

1 Answers1

0

This problem is not related to DevExpress directly. It rather relates to the common MVVM concepts. You said that you are using MVVM, but from your code I see that you've defined the Name2 property in the View and retargeted all the bindings within this View onto this View instance using the DataContext = this; line. I believe this is wrong way and you should learn more about the MVVM concepts before continue.

As far as I know about the DevExpress MVVM framework, the binding, demonstrated in your code won't work at runtime, because the DataContext property of this View will be assigned to corresponding ViewModel instance when application starts. Thus the correct approach is:

  1. Remove the DataContext = this; line
  2. Define the Name2 property at the corresponding ViewModel level - all the bindings will work as expected.

If you are still want to use the wrong approach, but make it works, use the relative binding (but you should also remove the DataContext = this; line !!!):

<TextBlock Text="{Binding Name2, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"/>

P.S. Please use the DevExpress Support Center if you find an issue in DevExpress products or run into a problem when using it.

DmitryG
  • 17,677
  • 1
  • 30
  • 53