locked
Using ViewModel Properties in the Code-Behind RRS feed

  • Question

  • I have a string property (its value is changing over time based on button presses) in the ViewModel and need to know how to use this string in the code-behind ?

    In other words, how can I make a string in the .xaml.cs file which reflects the value of that property in the ViewModel?

    I know ViewModel is used to avoid code-behind but my situation is a special case.

    Friday, July 29, 2011 10:18 PM

Answers

  •   public partial class MainWindow : Window
      {
        public string MyString
        {
          get { return (string)GetValue(MyStringProperty); }
          set { SetValue(MyStringProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for MyString. This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyStringProperty =
          DependencyProperty.Register("MyString", typeof(string), typeof(MainWindow), new UIPropertyMetadata(null));
    
        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
          if (e.Property == MyStringProperty)
          {
            Title = MyString; // if MyString changed i set Title to visualize
          }
          base.OnPropertyChanged(e);
        }
    
        public MainWindow()
        {
          InitializeComponent();
    
          //setup binding
          var binding = new Binding("ActualWidth");
          binding.Mode = BindingMode.OneWay; // make TwoWay if needed. Not for this example
          SetBinding(MyStringProperty, binding);
    
          // some data context. No matter
          DataContext = this;
        }
      }
    

    You can set any DataContext for it. It is just binding but in code. MyString is ActualWidth of MainForm everytime
    • Marked as answer by Adel K Sunday, July 31, 2011 9:27 PM
    Saturday, July 30, 2011 8:31 PM

All replies

  • You simply name the control.

    <TextBox X:Name = XTB/>
    

     

    And in code behind do this:

    string TextValue = XTB.Text;
    

     

     

     


    JP
    Friday, July 29, 2011 10:37 PM
  • what about TextBox's that are in a DataTemplate?
    Saturday, July 30, 2011 7:03 AM
  • Is your Window's DataContext set to the ViewModel's object?

    ((MyViewModel)(this.DataContext)).MyProperty
    


    bassem-mohsen.com
    • Proposed as answer by dEVIANT22 Saturday, July 30, 2011 2:39 PM
    Saturday, July 30, 2011 7:48 AM
  • Yes in the constructor of the Window I have this: 

    DataContext = new ServerViewModel();        

    I cant make instance like this:

    <Window.DataContext>

    <v:ServerViewModel x:Name="MyViewMoldel".....>

    <Window.DataContext>

    because I think the constructor of the view model has too many instances

    Saturday, July 30, 2011 8:38 AM
  • Great! Then, like I said in my previous post, you can get the instance from the Window.DataContext property, cast it to your ViewModel type, and access the ViewModel properties.

    If you are setting the DataContext from your XAML code, and setting the x:Name property like you showed in your previous post, then you can use the x:Name property value to refer to the ViewModel object in the code behind.

    If you are setting the DataContext from the constructor, you can also set a private field of your Window class to the ViewModel instance. This will make accessing the instance from the code behind easy. Easier than getting it from the DataContext and casting.


    bassem-mohsen.com
    Saturday, July 30, 2011 12:34 PM
  • You can create Dependency Property in your control and init binding in codebehind. But previous will work to
    Saturday, July 30, 2011 2:49 PM
  • I need example (or code lines) to understand !
    Saturday, July 30, 2011 3:03 PM
  •   public partial class MainWindow : Window
      {
        public string MyString
        {
          get { return (string)GetValue(MyStringProperty); }
          set { SetValue(MyStringProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for MyString. This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyStringProperty =
          DependencyProperty.Register("MyString", typeof(string), typeof(MainWindow), new UIPropertyMetadata(null));
    
        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
          if (e.Property == MyStringProperty)
          {
            Title = MyString; // if MyString changed i set Title to visualize
          }
          base.OnPropertyChanged(e);
        }
    
        public MainWindow()
        {
          InitializeComponent();
    
          //setup binding
          var binding = new Binding("ActualWidth");
          binding.Mode = BindingMode.OneWay; // make TwoWay if needed. Not for this example
          SetBinding(MyStringProperty, binding);
    
          // some data context. No matter
          DataContext = this;
        }
      }
    

    You can set any DataContext for it. It is just binding but in code. MyString is ActualWidth of MainForm everytime
    • Marked as answer by Adel K Sunday, July 31, 2011 9:27 PM
    Saturday, July 30, 2011 8:31 PM