C# 如何从UserControl动态指定绑定';当存在中间用户控件时,是否将属性设置为我的一个对象?
我想创建一个用户控件,用户可以在其中指定应用程序的设置。我创建了一个表示设置的C# 如何从UserControl动态指定绑定';当存在中间用户控件时,是否将属性设置为我的一个对象?,c#,wpf,silverlight,xaml,binding,C#,Wpf,Silverlight,Xaml,Binding,我想创建一个用户控件,用户可以在其中指定应用程序的设置。我创建了一个表示设置的CheckBoxPreference控件。我希望每个设置映射到MySettings中的一个属性(该属性反过来映射到IsolatedStorage) 理想情况下,我可以在CheckBoxPreference中指定一个键属性,该属性将映射到相同名称的属性值。例如,下面我在MySettings中指定了Key=“PlayAppAudio”和一个名为PlayAppAudio的属性 如何将PlayAppAudio属性映射到MySe
CheckBoxPreference
控件。我希望每个设置映射到MySettings
中的一个属性(该属性反过来映射到IsolatedStorage
)
理想情况下,我可以在CheckBoxPreference
中指定一个键属性,该属性将映射到相同名称的属性值。例如,下面我在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>
隐藏以下代码:
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");
}
}
}
将
CheckBoxPreference
的复选框绑定到新属性self->IsChecked
,并添加侦听方法,该方法将侦听键
属性更改并相应更改IsChecked
以下是有关如何侦听PropertyChanged事件的链接:
[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");
}
}
}