WPF绑定组合框<->应用程序设置

本文关键字:应用程序 设置 绑定 组合 WPF | 更新日期: 2023-09-27 18:06:18

对于我的wpfapplication,我添加了一个设置文件,其中包含一个Color作为设置:

MySetting.settings

Name      Type                         Value
myColor   System.Windows.Media.Color   #FFFFFF
所以我的自动生成的代码看起来像:

MySettings.Desinger.cs

    [global::System.Configuration.UserScopedSettingAttribute()]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Configuration.DefaultSettingValueAttribute("#FFFFFFFF")]
    public global::System.Windows.Media.Color myColor {
        get {
            return ((global::System.Windows.Media.Color)(this["myColor"]));
        }
        set {
            this["myColor"] = value;
        }
    }

我想在配置窗口上设置这个值。这是我的xaml代码:

configWindow.xaml

<Window x:Class="myApp.configWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:properties="clr-namespace:myApp.Properties"
    xmlns:converter="clr-namespace:myApp.Converter"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"      
    Title="myApp" Height="557" Width="626">
<Window.Resources>
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    <converter:myColorConverter x:Key="ColorConverter"/>
    <properties:MySettings x:Key="config"/>
    <ObjectDataProvider ObjectInstance="{x:Type Colors}" MethodName="GetProperties" x:Key="colorPropertiesOdp" />
</Window.Resources>
<Grid Height="524" Width="615" DataContext="{StaticResource config}">
    <TabControl Height="508" HorizontalAlignment="Left" Name="tabControl1" VerticalAlignment="Top" Width="616" BorderThickness="0" Margin="1,11,0,0">
        <TabItem Header="options" Name="tabItemOptions">
            <Grid Height="484">
                <GroupBoxHeight="330" Margin="6,149,15,0" Name="groupBox2" >
                    <Grid Height="313">
                        <ComboBox x:Name="comboBoxMyColor" Height="23" HorizontalAlignment="Left" Margin="302,34,0,0" VerticalAlignment="Top" Width="120" ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}" SelectedItem="{Binding Path=Default.myColor, Converter={StaticResource ColorConverter}, Mode=TwoWay}">
                            <ComboBox.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <Label Background="{Binding Path=Name}" Content="{Binding Path=Name}" Height="{Binding ActualHeight, ElementName=comboBoxNotificationColor}" Width="{Binding ActualWidth, ElementName=comboBoxNotificationColor}"/>
                                    </StackPanel>
                                </DataTemplate>
                            </ComboBox.ItemTemplate>
                        </ComboBox>
                    </Grid>
                </GroupBox>
            </Grid>
        </TabItem>
    </TabControl>
</Grid>

但是如果我尝试改变颜色,我可以选择一个值(例如:"白色")上的组合框,但配置永远不会改变。当我在MySettings.Desinger.cs -> myCOlor -> set { ... }上设置断点时,它永远不会到达。我的错在哪里?

WPF绑定组合框<->应用程序设置

你错过了INotifyPropertyChanged在我看来

没有它,你的模型或视图模型不能通知当你从ui改变一些东西。

相关信息在这里:WPF-INotifyPropertyChanged