【问题标题】:Binding value from parent Page ViewModel to UserControl将值从父 Page ViewModel 绑定到 UserControl
【发布时间】:2019-11-27 06:46:23
【问题描述】:

我编写了新的 WPF MVVM 应用程序。 我是 WPF 的新手。 我在将值从 MainPageModelView 绑定到 StageControl 时遇到问题。 StageControl 在 MainPage 中。 我知道如何将值绑定到 MainPage 中的元素,但我不能以同样的方式将值绑定到 StageControl。 如何将值从 MainPageModelView 绑定到 StageControl? 代码:

MainPage.xaml

<my:StageControl x:Name="stageControl1" StageIsActive="true"  StageName="{Binding Stage.Name}" Grid.Row="0" Grid.Column="0"/>
...
<Label x:Name="lbTest" Content="{Binding Test}" HorizontalAlignment="Left" Margin="104,10,0,0" VerticalAlignment="Top" Height="56" Width="68"/>

StageControl.xaml.cs

public partial class StageControl : UserControl
    {
        string stageName;
        bool stageIsActive;

        public StageControl()
        {
            InitializeComponent();
        }

        public bool StageIsActive
        {
            get { return this.stageIsActive; }
            set { this.stageIsActive = SetStageControlStatus(value); }
        }

        public string StageName
        {
            get { return this.stageName; }
            set { this.stageName = SetStageName(value); }
        }

        private bool SetStageControlStatus(bool value)
        {
            if (value)
            {
                this.outRing.Visibility = Visibility.Visible;
                return true;
            }
            else
            {
                this.outRing.Visibility = Visibility.Hidden;
                return false;
            }    
        }

        private string SetStageName(string value)
        {
            this.text.Text = value;
            return this.text.Text;
        }
    }

MainPageViewModel.cs

class MainPageViewModel
    {
        public List<Stage> Stages = new List<Stage>();
        public Stage stage = new Stage(0, "Test", true);

        public MainPageViewModel()
        {
            Stages = Stage.GetStages();
        }

        public string Test
        {
            get { return "Testowy Label"; }
            set { }
        }

    }

编辑: MainPage.xaml.css

public MainPage()
        {
            InitializeComponent();
            MainPageViewModel viewModel = new MainPageViewModel();
            this.DataContext = viewModel;
        }

【问题讨论】:

  • StageName 必须是依赖属性
  • UserControl 如果使用正确,将继承 DataContext 并可以访问底层数据。还有Page 不继承DataContext!!!将 ContentControl 或 ItemsControl 与 DataTemplate 一起使用以保留继承。查看需要实施 INotifyPropertyChanged 以获取更改。
  • 您只能为依赖属性设置绑定,而 StageName 不能。
  • 我添加了依赖属性(public static readonly DependencyProperty StageNameProperty = DependencyProperty.Register("StageName", typeof(string), typeof(StageControl));)现在 StageControl 有我设置为默认值的文本/ StageControl.xaml 中的模板

标签: c# .net wpf mvvm data-binding


【解决方案1】:
public Stage Stage {get;set;} = new Stage(0,"Test",true);

你需要创建属性而不是公共变量

【讨论】:

    【解决方案2】:

    我解决了这个问题。 首先我将依赖属性添加到 StageControl.xaml.cs,然后我将绑定添加到 StageControl.xaml

    ...
    x:Name="Stage"
    ...
    <TextBlock x:Name="text" TextWrapping="Wrap" Text="{Binding ElementName=Stage, Path=StageName}" TextAlignment="Center"  FontSize="12" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    
    

    【讨论】:

      猜你喜欢
      • 2011-07-19
      • 2016-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-29
      • 2016-07-11
      • 1970-01-01
      • 2011-10-31
      相关资源
      最近更新 更多