【发布时间】:2013-04-15 14:06:49
【问题描述】:
有一个实现 INotifyPropertyChanged 接口的类,如下所示:
public class ColorNotify : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private SolidColorBrush bindableColor;
public SolidColorBrush BindableColor
{
get { return bindableColor; }
set
{
bindableColor = value;
OnPropertyChanged("BindableColor");
}
}
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
我的 MainPage.xaml.cs 中的一个字段是这样的:
private ColorNotify DisabledColor;
但我也尝试将上述字段作为属性,而不是像这样:
private ColorNotify disabledColor;
public ColorNotify DisabledColor
{
get
{ return disabledColor; }
set
{ disabledColor = value; }
}
MainPage.xaml.cs 中还有这个方法:
private void Legend_Refreshed(object sender, Legend.RefreshedEventArgs Lgd_Refreshed_EvArgs)
{
LayerItemViewModel layerItmViewMdl;
ObservableCollection<LayerItemViewModel> layerItms;
layerItmViewMdl = Lgd_Refreshed_EvArgs.LayerItem;
Layer legendItemLyr = layerItmViewMdl.Layer;
if (!legendItemLyr.IsInitialized)
{
DisabledColor = new ColorNotify();
DisabledColor.BindableColor = new SolidColorBrush(Colors.Red);
}
...
...
}
在 MainPage.xaml 我有这个:
<esri:Legend
Map="{Binding ElementName=theMap}"
LayerIDs="..."
Refreshed="Legend_Refreshed">
<esri:Legend.MapLayerTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="Auto">
<CheckBox Content="{Binding Label}"
Width="Auto"
IsChecked="{Binding IsEnabled, Mode=TwoWay}"
IsEnabled="{Binding IsInScaleRange}"
Foreground="{Binding Source=DisabledColor, Path=BindableColor}">
</CheckBox>
<Slider Maximum="1"
Value="{Binding Layer.Opacity, Mode=TwoWay}"
Width="80" />
</StackPanel>
</DataTemplate>
...
...
...
</esri:Legend>
注意上面的 XAML 代码:
Foreground="{Binding Source=DisabledColor, Path=BindableColor}">
那没用。
然后我在 XAML 中尝试了这个:
Foreground="{Binding BindableColor}"
但这也没用 如何将 Foreground 绑定到 ColorNotify 类的 BindableColor 属性? 绑定到 INotifyPropertyChanged 对象时,XAML 绑定应该是什么样子? (不通过 MVVM)
【问题讨论】:
-
将该字段设为属性并将
DisabledColor.BindableColor作为您的路径 -
顺便说一句,我已经尝试查看此链接以寻找类似的问题,但在那里我没有看到解决方案。 stackoverflow.com/questions/1962149/…
-
NSGaga,你的意思是我应该把“ColorNotify DisabledColor”变成一个属性吗?绑定应该是:“{Binding Path=DisabledColor.Bindablecolor}”?
-
@NSGaga,我尝试了以下方法:'Foreground="{Binding Path=DisabledColor.Bindablecolor}',我将属性设置为:'private ColorNotify disabledColor; public ColorNotify DisabledColor { get { return disabledColor ; } set { disabledColor = value; } }' 但这不起作用。
-
在
set上执行OnNotifyChanged(并正确实施)-看看其他人的建议-或让我知道
标签: c# silverlight xaml