我有一个在下拉列表中渲染的对象:
[DataContract]
public sealed class ConfiguredFileViewModel : Model
{
[DataMember] public string Path { get; set; }
[DataMember] public TemplateViewModel Template { get; set; }
[DataMember] public List<TemplateViewModel> AvailableTemplates { get; set; }
}
public class TemplateViewModel : INotifyPropertyChanged
{
private string _name;
private List<KeyValueViewModel> _properties;
// Rest of getters setters
}
<!-- Foreach ConfiguredFile, render TextBlocks and TextInputs -->
<ItemsControl ItemsSource="{Binding ConfiguredFiles}" Margin="10,20,0,0">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Margin="10">
<!-- File Path -->
<TextBlock Text="{Binding Path}" FontWeight="Bold" VerticalAlignment="Center" Margin="0,0,0,5"/>
<!-- Telerik RadComboBox for Templates -->
<telerik:RadComboBox ItemsSource="{Binding AvailableTemplates}"
SelectedItem="{Binding Template, Mode=TwoWay}"
DisplayMemberPath="Name"
HorizontalAlignment="Left"
Width="275"
Margin="0,5"/>
<!-- Render Template Properties Based on Selected Template -->
<ItemsControl HorizontalAlignment="Left" Margin="0,0,0,0" ItemsSource="{Binding Template.Properties}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,5">
<TextBlock Text="{Binding Key}" VerticalAlignment="Center" Width="120"/>
<TextBox Text="{Binding Value, Mode=TwoWay}" Width="150" HorizontalAlignment="Left" Margin="5,0"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
当我填写
Properties
的 Template
并保存 ConfiguredFileViewModel
时,加载应用程序备份,我可以在调试器中看到,该对象有一个 Path
和一个更改后的 Template
Properties
。但即使名称相同,UI 也不会显示下拉列表预选 Template
。
只有当我将
Template
设置为 AvailableTemplates
的对象时,它才会预选,但我不希望它加载默认属性。有人知道解决方法吗?
我尝试过:
SelectedValue
IsSynchronizedWithCurrentItem
,但这只是更改了默认属性(为什么我的 ComboBox SelectedItem 为空?)Mode=TwoWay
也没什么作用我刚刚修改了
ConfiguredFileViewModel
类:
[DataContract]
public class ConfiguredFileViewModel : Model, INotifyPropertyChanged
{
private string _path;
private TemplateViewModel _template;
private List<CustomKeyValue> _properties;
[DataMember]
public string Path
{
get => _path;
set { _path = value; OnPropertyChanged(nameof(Path)); }
}
[DataMember]
public TemplateViewModel SelectedTemplate // One of the default Templates
{
get => _template;
set
{
if (_template != value)
{
_template = value;
OnPropertyChanged(nameof(SelectedTemplate));
var newKeys = _template?.Properties.Select(kv => kv.Key);
var oldKeys = Properties?.Select(kv => kv.Key) ?? Enumerable.Empty<string>();
if (!new HashSet<string>(newKeys).SetEquals(oldKeys))
{
// Load template properties with default values when the template changes (= when keys become unequal)
Properties = _template?.Properties
.Select(kv => new CustomKeyValue { Key = kv.Key, Value = kv.Value })
.ToList();
}
}
}
}
[DataMember]
public List<CustomKeyValue> Properties // Changed or default properties
{
get => _properties;
set { _properties = value; OnPropertyChanged(nameof(Properties)); }
}
[DataMember]
public List<TemplateViewModel> AvailableTemplates { get; set; } // Templates with default properties
// Rest of INotifyPropertyChanged code
}