如何从UserControl动态指定绑定';s属性,当存在中间UserControl时

本文关键字:UserControl 属性 存在 在中间 动态 绑定 | 更新日期: 2024-06-14 01:40:58

我想创建一个用户控件,用户可以在其中指定应用程序的设置。我已经创建了一个代表设置的CheckBoxPreference控件。我希望每个设置都映射到MySettings中的一个属性(依次映射到IsolatedStorage)。

理想情况下,我可以在CheckBoxPreference中指定一个Key属性,该属性将映射到相同名称的属性值。例如,下面我在MySettings 中指定了Key="PlayAppAudio"和一个名为PlayAppAudio的属性

如何获取PlayAppAudio属性以映射到MySettings.PlayAppAudio

到目前为止,我的代码如下:

MainPage.xaml包含:

        <StackPanel>
            <my:CheckBoxPreference 
                Title="Play Application Audio" 
                Summary="Play application audio events"
                DefaultValue="True" 
                Key="PlayAppAudio" />
            <my:CheckBoxPreference 
                Title="Allow Vibrations" 
                Summary="Allow device to vibrate"
                DefaultValue="True" 
                Key="AllowVibrations" />
        </StackPanel>

Key属性是我想要映射的属性。

CheckBoxPreference定义为:

        <StackPanel VerticalAlignment="Top" >
            <TextBlock Name="txtTitle" Text="{Binding Path=Title, ElementName=MyUserControl}" FontSize="30"/>
            <TextBlock Name="txtDescription" Text="{Binding Path=Summary, ElementName=MyUserControl}" FontSize="20"/>
        </StackPanel>
        <!--<CheckBox 
            IsChecked="{Binding Source={StaticResource MySettings}, 
            Path=PlayAppAudio, Mode=TwoWay}" 
            Grid.Column="1"/>-->
        <CheckBox Name="Bindable"             
            Grid.Column="1"/>

后面有以下代码:

public partial class CheckBoxPreference : UserControl
{
    public CheckBoxPreference()
    {
        InitializeComponent();
    }
    public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(String), typeof(CheckBoxPreference), null);
    public static readonly DependencyProperty SummaryProperty = DependencyProperty.Register("Summary", typeof(String), typeof(CheckBoxPreference), null);
    public static readonly DependencyProperty KeyProperty = DependencyProperty.Register("Key", typeof(String), typeof(CheckBoxPreference), null);
    public static readonly DependencyProperty CheckBoxProperty = DependencyProperty.Register("DefaultValue", typeof(bool), typeof(CheckBoxPreference), null);
    public String Title
    {
        get { return GetValue(TitleProperty).ToString(); }
        set { SetValue(TitleProperty, value); }
    }
    public String Summary
    {
        get { return GetValue(SummaryProperty).ToString(); }
        set { SetValue(SummaryProperty, value); }
    }
    public String Key
    {
        get { return GetValue(KeyProperty).ToString(); }
        set { SetValue(KeyProperty, value); }
    }
    public bool DefaultValue
    {
        get { return (bool)GetValue(CheckBoxProperty); }
        set { SetValue(CheckBoxProperty, value); }
    }
}

MySettings有我想映射到的属性,定义为

    [DefaultValue(true)]
    public bool PlayAppAudio
    {
        get { return _playAppAudio; }
        set
        {
            bool old = _playAppAudio;
            _playAppAudio = value;
            if (value != old)
            {
                NotifyPropertyChanged("PlayAppAudio");
            }
        }
    }
    [DefaultValue(true)]
    public bool AllowVibrations
    {
        get { return _allowVibrations; }
        set
        {
            bool old = _allowVibrations;
            _allowVibrations = value;
            if (value != old)
            {
                NotifyPropertyChanged("AllowVibrations");
            }
        }
    }

如何从UserControl动态指定绑定';s属性,当存在中间UserControl时

CheckBoxPreference的复选框绑定到新属性self->IsChecked,并添加将侦听Key属性更改的侦听方法,并相应地更改IsChecked。。。

以下是有关如何侦听PropertyChanged事件的链接:http://blogs.msdn.com/b/llobo/archive/2007/03/05/listening-to-dependencyproperty-changes.aspx