有一个实现 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)