我有一个使用ObjectDataProvider (App.xaml)的应用程序:
<Application x:Class="Example.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:Example.Settings"
StartupUri="MainWindow.xaml"
Startup="Application_Startup"
DispatcherUnhandledException="ApplicationDispatcherUnhandledException">
<Application.Resources>
<ObjectDataProvider x:Key="odpSettings" ObjectType="{x:Type src:AppSettings}"/>
</Application.Resources>
我的课程是:
class AppSettings : INotifyPropertyChanged
{
public AppSettings()
{
}
Color itemColor = Colors.Crimson;
public Color ItemColor
{
get
{
return itemColor;
}
set
{
if (itemColor == value)
return;
itemColor = value;
this.OnPropertyChanged("ItemColor");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
} }
然后我有一个使用该颜色的userControl,例如:
<Border Background="{Binding Source={StaticResource odpSettings},
Path=ItemColor,Mode=TwoWay}" />我正在将该UserControl添加到我的MainWindow中,其中我有一个ColorPicker控件,并且我想用所选的ColorPicker颜色修改我的UserControl的边框背景颜色。
我尝试了这样的东西:
AppSettings objSettings = new AppSettings();
objSettings.ItemColor = colorPicker.SelectedColor;当我使用ColorPicker更改颜色时,我的UserControl中的颜色没有更改,我猜这是因为我正在创建AppSettings类的一个新实例。
有没有办法完成我想要做的事情?
提前谢谢。
阿尔贝托
发布于 2014-08-08 07:18:58
谢谢你的评论,我使用了下面的代码:
AppSettings objSettings = (AppSettings)((ObjectDataProvider)Application.Current.FindResource("odpSettings")).ObjectInstance;这样我就可以访问和修改属性ItemColor的值。
我还将属性类型更改为SolidColorBrush。
objSettings.ItemColor = new SolidColorBrush(colorPicker.SelectedColor);https://stackoverflow.com/questions/25165647
复制相似问题