查看ViewModel到设置

时间:2016-04-22 17:06:51

标签: c# wpf xaml mvvm application-settings

是否可以将currentDevices重构为集合? 基本上,我有三个组合框,其中selectedvalue绑定到currentDevices。然后从设置文件中获取currentDevices。

查看

<ComboBox ItemsSource="{Binding availableDevices}"
          SelectedValue="{Binding currentDevice1}">
</ComboBox>
<ComboBox ItemsSource="{Binding availableDevices}"
          SelectedValue="{Binding currentDevice2}">
</ComboBox>
<ComboBox ItemsSource="{Binding availableDevices}"
          SelectedValue="{Binding currentDevice3}">
</ComboBox>

视图模型

public string currentDevice1 {
    get
    {
        return SampleSettings.Default.Device1;
    }
    set
    {
        SampleSettings.Default.Device1 = value;
    }

}
public string currentDevice2
{
    get
    {
        return SampleSettings.Default.Device2;
    }
    set
    {
        SampleSettings.Default.Device2 = value;
    }
}
public string currentDevice3
{
    get
    {
        return SampleSettings.Default.Device3;
    }
    set
    {
        SampleSettings.Default.Device3 = value;
    }
}

1 个答案:

答案 0 :(得分:2)

我写了一个DeviceOptionViewModel,并给主视图模型一个ObservableCollection。

DeviceOptionViewModel.cs

public class DeviceOptionViewModel : INotifyPropertyChanged 
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string _currentDevice;
    public String CurrentDevice {
        get { return _currentDevice; }
        set { 
            _currentDevice = value; 
            PropertyChanged?.Invoke(this, 
                new PropertyChangedEventArgs(nameof(CurrentDevice));
        }
    }

    //  Parent event assigns this to his own availableDevices
    //  when he creates this.
    public IEnumerable AvailableDevices { get; set; }
}

主虚拟机:

    public ObservableCollection<DeviceOptionViewModel> 
        CurrentDevices { get; private set; }
            = new ObservableCollection<DeviceOptionViewModel>();

XAML:

<ItemsControl
    ItemsSource="{Binding CurrentDevices}"
    >
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <!-- DataContext here is DeviceOptionViewModel. We gave it its 
                 own reference to AvailableDevices to simplify binding. -->
            <ComboBox 
                ItemsSource="{Binding AvailableDevices}"
                SelectedValue="{Binding CurrentDevice}"
                />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

返回主视图模型:

protected void PopulateCurrentDevices(IEnumerable<String> stringsFromWherever)
{
    CurrentDevices.Clear();

    foreach (var device in stringsFromWherever)
    {
        var dovm = new DeviceOptionViewModel() {
                CurrentDevice = device,
                AvailableDevices = this.availableDevices
            };

        dovm.PropertyChanged += DeviceOptionViewModel_PropertyChangedHandler;

        CurrentDevices.Add(dovm);
    }
}

protected void DeviceOptionViewModel_PropertyChangedHandler(object sender, 
     PropertyChangedEventArgs e)
{
    var dopt = sender as DeviceOptionViewModel;

    if (e.PropertyName == nameof(DeviceOptionViewModel.CurrentDevice))
    {
        //  Do stuff
    }
}

因此,您根据需要在视图模型中填充并重新填充CurrentDevices,如果所有通知都正确完成,UI将会神奇地出现。

如果您创建新的ObservableCollection并将其分配给CurrentDevices媒体资源,则需要在主视图模型上引发PropertyChanged(nameof(CurrentDevices))。我将setter设为私有,以避免必须实现该细节。如果它不是一个庞大的集合,也可以在同一个旧实例上Clear()Add()