有一個實現INotifyPropertyChanged接口這樣的類:如何做一個XAML屬性綁定到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}"
但是,這並沒有工作,要麼 如何綁定前景到ColorNotify類的BindableColor財產? 綁定到INotifyPropertyChanged對象時,XAML綁定應該如何? (不通過MVVM)
作出這樣的字段的屬性,並做'DisabledColor.BindableColor'作爲您的路徑 – NSGaga 2013-04-22 15:40:20
順便說一句,我已經嘗試尋找這個鏈接的類似問題,但我沒有看到一個解決方案。 http://stackoverflow.com/questions/1962149/how-can-i-bind-a-background-color-in-wpf-xaml?rq=1 – Beebok 2013-04-22 15:46:21
NSGaga,你是說我應該把「ColorNotify DisabledColor」變成屬性?如果綁定是:「{Binding Path = DisabledColor.Bindablecolor}」? – Beebok 2013-04-22 15:51:52