MVVM WPF C#自动属性组合框

时间:2017-01-09 09:04:22

标签: c# wpf mvvm combobox

我想使用Propertychanged.Fody重构我的代码,如本页所示http://www.mutzl.com/tag/mvvm-light/

普通代码:

private string _platformSelectedItem;
        public string PlatformSelectedItem
        {
            get { return _platformSelectedItem; }
            set
            {
                if (_platformSelectedItem == value) return;
                _platformSelectedItem = value;
                // Perform any pre-notification process here.
                GetData();
                RaisePropertyChanged();
            }
        }

public string PlatformSelectedItem {get; private set}

该属性受限于Comboxbox,并且Combobox的值基于另一个组合框是动态的,因此我有我的方法GetData()。

<ComboBox ItemsSource="{Binding Platforms}" SelectedItem="{Binding PlatformSelectedItem, Mode=TwoWay}"  Grid.Column="1" Grid.Row="2" Height="20" Grid.ColumnSpan="2" Margin="0,3,15.667,3"/>

如果我将代码重构为autoproperties,则必须通过单击/打开组合框来执行该方法。

我应该在命令中使用eventtrigger,我们的方法更简单吗?

1 个答案:

答案 0 :(得分:0)

基于帖子can we use <i:Interaction.Triggers> in WPF MVVM (not in Silverlight)

我的最终解决方案是:

视图模型:

属性区域:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<form>
<p>
<span>Enter you name</span>
<input type="text" name="nothing" id=""/>
</p>
<p>
<input type="text" name="abc" id=""/>
</p>
<p>
<input type="submit"  id="sub" value="SUBMIT ME SIR" />
</p>
</form>

构造

public RelayCommand SelectionChangedCommand { get; private set; }

方法区:

SelectionChangedCommand = new RelayCommand(Update);

在我的用户界面中:

   private void Update()
    {
        GetData();
    }