【发布时间】:2020-02-19 04:38:05
【问题描述】:
我正在创建一个 wpf 窗口来编辑用户设置。
这是我到目前为止所做的:
<ListView Grid.Row="1"
ItemsSource="{Binding Source={x:Static properties:Settings.Default}, Path=PropertyValues}"
HorizontalContentAlignment="Stretch" Background="LightGray"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListView.ItemTemplate>
<DataTemplate>
<DockPanel HorizontalAlignment="Stretch"
IsEnabled="{Binding DataContext.Enabled, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}">
<Label Width="200" Content="{Binding Name}"/>
<Label Width="200" Content="{Binding Path=Property.PropertyType}" Foreground="Gray" FontStyle="Italic"/>
<ContentControl VerticalContentAlignment="Center" Content="{Binding Path=PropertyValue}">
<ContentControl.Resources>
<ResourceDictionary>
<DataTemplate DataType="{x:Type sys:Boolean}">
<CheckBox IsChecked="{Binding Path=., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type sys:String}">
<TextBox Text="{Binding Path=., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type sys:Int32}">
<TextBox Text="{Binding Path=., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</ResourceDictionary>
</ContentControl.Resources>
</ContentControl>
</DockPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
这些值正在显示,但当我使用Properties.Settings.Default.Save(); 更改和保存它们时,它们没有在Properties.Settings.Default 中更新。
双向绑定是否正确?
谢谢
【问题讨论】:
-
你的模型是否实现了 INotifyPropertyChanged?
-
System.Configuration.SettingsPropertyValue没有 -
双向绑定应该获取/设置源属性的值。使用
Path=.直接绑定到对象不会神奇地替换该对象。 -
能否提供一下背后的代码?
-
我按照这个答案 (stackoverflow.com/a/57300798/1121245) 中的建议做了,并创建了一个 ViewModel 来封装
System.Configuration.SettingsPropertyValue对象
标签: c# wpf data-binding