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

时间:2013-03-27 23:03:29

标签: c# data-binding combobox settings application-settings

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

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 { ... }上设置断点时,它永远不会到达。 我的错在哪里?

1 个答案:

答案 0 :(得分:1)

我认为你错过了INotifyPropertyChanged。

如果没有它,当你从ui更改某些内容时,你的模型或viewmodel就无法通知。

此处的相关信息:WPF-INotifyPropertyChanged