gpt4 book ai didi

c# - 查看到 ViewModel 到设置

转载 作者:行者123 更新时间:2023-12-03 10:27:06 25 4
gpt4 key购买 nike

是否可以将 currentDevices 重构为一个集合?
基本上,我有三个组合框,其中 selectedvalue 绑定(bind)到 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>

View 模型
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;
}
}

最佳答案

我会写一个 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根据需要在您的 View 模型中,如果所有通知都正确完成,用户界面将神奇地出现。

如果您创建一个新的 ObservableCollection并将其分配给 CurrentDevices属性(property),你需要提高 PropertyChanged(nameof(CurrentDevices))在主视图模型上。我将 setter 设为私有(private)以避免必须实现该细节。如果不是一个庞大的集合,不妨只是 Clear()Add()在同一个旧实例上。

关于c# - 查看到 ViewModel 到设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36799633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com