none
请教大神,xaml前台怎样才更有效调用、绑定后台变量? RRS feed

  • 问题

  • 请教大神,xaml前台怎样才更有效调用、绑定后台变量,在后台使用DataContext?可以不需要指定DataContext的情况下,从xaml绑定吗???什么样的才最有效?
    2016年2月17日 17:27

答案

  • 您好 Max903船长,

    >>可以不需要指定DataContext的情况下,从xaml绑定吗???

    可以的。除了可以从DataContext获取数据以下。WPF的绑定还可以从另一个控件获取数据。 我们的窗体就是一个特殊的控件。 我们需要在后台代码中为窗体增加依赖属性。下面是我写的例子,有个Window6窗体,声明了一个NewProp依赖属性。

    public int NewProp
    {
        get { return (int)GetValue(NewPropProperty); }
        set { SetValue(NewPropProperty, value); }
    }
    
    // Using a DependencyProperty as the backing store for NewProp.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty NewPropProperty =
    DependencyProperty.Register("NewProp", typeof(int), typeof(Window6), new PropertyMetadata(0));

    然后就可以在XAML中绑定NewProp这个属性了。

    <TextBox Text="{Binding NewProp,ElementName=window6}" />

    >> "请教大神,xaml前台怎样才更有效调用、绑定后台变量?"

    在WPF中,推荐使用MVVM架构。其中V和VM分别代表View(视图)和View Model(视图模型)。 我们需要把视图中需要绑定的变量都存储在View Model(视图模型)中,而且View Model需要实现INotifyPropertyChanged接口。然后将视图模型赋值给当前窗体的DataContext属性。

    比如我的视图中需要显示姓名和年龄两项内容,那么我的View Model需要写成这个样子:
    public class MyViewModel : INotifyPropertyChanged
    {
        private string _name;
    
        public string Name
        {
            get { return _name; }
            set
            {
                if (_name != value)
                {
                    _name = value;
                    RaisePropertyChanged(Name);
                }
            }
        }
    
        private int _age;
    
        public int Age
        {
            get { return _age; }
            set
            {
                if (_age != value)
                {
                    _age = value;
                    RaisePropertyChanged("Age");
                }
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public void RaisePropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    }

    XAML中绑定可以这么写:

    <StackPanel>
        <TextBox Text="{Binding Name}"/>
        <TextBox Text="{Binding Age}"/>
    </StackPanel>

    最后关联View(视图)和View Model(视图模型)。

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        MyViewModel1 vm = new MyViewModel1();
        vm.Name = "Zhang San";
        vm.Age = 19;
        this.DataContext = vm;
    }
    如果我的解释和代码不是您想要的,还请提供更加详细的描述和您的需求。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年2月19日 9:32
    版主

全部回复

  • DataContext是用于为界面元素提供数据上下文的,也就是一个数据环境,有了这个环境,WPF才能去寻找对应的目标,然后建立绑定关系。

    DataContext是可以继承的,子元素默认都会使用父级元素的DataContext,除非被另行指定,所以并不需要非常频繁的使用DataContext。

    DataContext也是可以通过绑定/资源等赋值方式在前台指定的,而不需要总在后台使用。

    考虑使用DataTemplate方式或MVVM模式来建立数据绑定,这样你就不需要显式指定DataContext了,而是更为直观的“数据对应视图”的概念。

    另外,建议提问时描述得更清楚一点,最好说清楚你的需求和现状以及你作过的尝试,这样才能让他人更加迅速的了解问题然后帮你解决。

    2016年2月19日 6:53
  • 您好 Max903船长,

    >>可以不需要指定DataContext的情况下,从xaml绑定吗???

    可以的。除了可以从DataContext获取数据以下。WPF的绑定还可以从另一个控件获取数据。 我们的窗体就是一个特殊的控件。 我们需要在后台代码中为窗体增加依赖属性。下面是我写的例子,有个Window6窗体,声明了一个NewProp依赖属性。

    public int NewProp
    {
        get { return (int)GetValue(NewPropProperty); }
        set { SetValue(NewPropProperty, value); }
    }
    
    // Using a DependencyProperty as the backing store for NewProp.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty NewPropProperty =
    DependencyProperty.Register("NewProp", typeof(int), typeof(Window6), new PropertyMetadata(0));

    然后就可以在XAML中绑定NewProp这个属性了。

    <TextBox Text="{Binding NewProp,ElementName=window6}" />

    >> "请教大神,xaml前台怎样才更有效调用、绑定后台变量?"

    在WPF中,推荐使用MVVM架构。其中V和VM分别代表View(视图)和View Model(视图模型)。 我们需要把视图中需要绑定的变量都存储在View Model(视图模型)中,而且View Model需要实现INotifyPropertyChanged接口。然后将视图模型赋值给当前窗体的DataContext属性。

    比如我的视图中需要显示姓名和年龄两项内容,那么我的View Model需要写成这个样子:
    public class MyViewModel : INotifyPropertyChanged
    {
        private string _name;
    
        public string Name
        {
            get { return _name; }
            set
            {
                if (_name != value)
                {
                    _name = value;
                    RaisePropertyChanged(Name);
                }
            }
        }
    
        private int _age;
    
        public int Age
        {
            get { return _age; }
            set
            {
                if (_age != value)
                {
                    _age = value;
                    RaisePropertyChanged("Age");
                }
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public void RaisePropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    }

    XAML中绑定可以这么写:

    <StackPanel>
        <TextBox Text="{Binding Name}"/>
        <TextBox Text="{Binding Age}"/>
    </StackPanel>

    最后关联View(视图)和View Model(视图模型)。

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        MyViewModel1 vm = new MyViewModel1();
        vm.Name = "Zhang San";
        vm.Age = 19;
        this.DataContext = vm;
    }
    如果我的解释和代码不是您想要的,还请提供更加详细的描述和您的需求。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年2月19日 9:32
    版主
  • 我想实现后台有变量public string Name="jack";,前台xaml如何在没有采用MVVM指定DataContext的情况下绑定这个变量?并且后台变量变化前台也会变(单向或双向绑定)?可以实现吗?比起MVVM方法谁更有效?

    2016年2月19日 14:11
  • 您好 Max903船长,

    >> "我想实现后台有变量public string Name="jack";,前台xaml如何在没有采用MVVM指定DataContext的情况下绑定这个变量?并且后台变量变化前台也会变(单向或双向绑定)?可以实现吗?"

    这样不可以实现的, WPF中的控件无法绑定到后台变量(字段),绑定的只能是属性。

    >> "比起MVVM方法谁更有效?"

    推荐您使用MVVM, MVVM让我们的代码更加的整洁,让我们的编程思路更加清晰。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年2月23日 2:07
    版主