[英]Silverlight: How do I update properties dynamically in accordance with a current settings class?
以下是該示例的完整代碼。 我有一個名為ColorPicker的用戶控件,它包含3個按鈕,每個按鈕顯示一種顏色。 單擊按鈕時,將設置CurrentSettings類中的Color屬性。 我想要發生的是MainPage上的矩形顏色要更改以匹配新的CurrentSettings.Color和第二個用戶控件的列表框中矩形的顏色(在后面的代碼中添加)以更改顏色以匹配新的CurrentSettings.Color。
我一直試圖使用Dependency Properties和INotifyPropertyChanged來完成這個失敗,現在又決定以干凈的方式重新開始。
// Current Sttings類:
public static class CurrentSettings
{
public static Color Color { get; set; }
}
// MainPage XAML
<Grid x:Name="LayoutRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="33*"/>
<ColumnDefinition Width="33*"/>
<ColumnDefinition Width="33*"/>
</Grid.ColumnDefinitions>
<local:ColorPicker/>
<Rectangle Grid.Column="1" Name="rec" Width="160" Height="80" Fill="Yellow"/>
<local:PenSelector Grid.Column="2"/>
</Grid>
// ColorPicker用戶控件XAML:
<StackPanel x:Name="LayoutRoot" Orientation="Horizontal">
<Button x:Name="Red" Width="40" Height="40" Click="Red_Click">
<Button.Content>
<Rectangle Width="30" Height="30" Fill="Red"/>
</Button.Content>
</Button>
<Button x:Name="Green" Width="40" Height="40" Click="Green_Click">
<Button.Content>
<Rectangle Width="30" Height="30" Fill="Green"/>
</Button.Content>
</Button>
<Button x:Name="Blue" Width="40" Height="40" Click="Blue_Click">
<Button.Content>
<Rectangle Width="30" Height="30" Fill="Blue"/>
</Button.Content>
</Button>
</StackPanel>
// ColorPicker用戶控制代碼背后:
public partial class ColorPicker : UserControl
{
public ColorPicker()
{
InitializeComponent();
}
private void Red_Click(object sender, RoutedEventArgs e)
{
CurrentSettings.Color = Colors.Red;
}
private void Green_Click(object sender, RoutedEventArgs e)
{
CurrentSettings.Color = Colors.Green;
}
private void Blue_Click(object sender, RoutedEventArgs e)
{
CurrentSettings.Color = Colors.Blue;
}
}
//筆選擇器用戶控件XAML:
<ListBox x:Name="LayoutRoot"/>
//筆選擇器用戶控件XAML代碼背后:
public partial class PenSelector : UserControl
{
public PenSelector()
{
InitializeComponent();
LayoutRoot.Items.Add(new Rectangle() { Width = 160, Height = 80, Fill = new SolidColorBrush(Colors.Yellow) });
LayoutRoot.Items.Add(new Rectangle() { Width = 160, Height = 80, Fill = new SolidColorBrush(Colors.Yellow) });
}
}
您使用INotifyPropertyChanged
處於正確的軌道上。 從設置類開始,但將Color設置為實現INotifyPropertyChanged
的類的實例屬性。
public class CurrentSettings : INotifyPropertyChanged
{
private Color _Color;
public Color Color
{
get { return _Color; }
set { _Color = value; NotifyPropertyChanged("Color"); }
}
private void NotifyPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name);
}
public event PropertyChangedEventHandler PropertyChanged;
}
現在在您的App.Xaml
的資源中放置一個這樣的實例: -
<Application.Resources>
<local:CurrentSettings x:Key="CurrentSettings" />
</Application.Resources>
現在將一個CurrentSettings
私有屬性添加到顏色選擇器: -
private CurrentSettings CurrentSettings
{
get
{
return (CurrentSettings)Application.Current.Resources["CurrentSettings"];
}
}
最后在矩形Fill
屬性上使用綁定如下: -
<Rectangle Grid.Column="1" Name="rec">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Color, Source={StaticResource CurrentSettings}}"/>
</Rectangle.Fill>
</Rectangle>
這里有很多移動部件 - 一個colorPicker用戶控件,它可以更改主窗體上的POCO,從而更新另一個用戶控件中添加的項目PenSelector - 如果我理解正確的話。 如果您將DP添加到您的用戶控件並將INotifyPropertyChanged添加到您的POCO然后將DP綁定到POCO公共屬性,您應該能夠進行所需的交互,並在這些部分之間進行一些解耦:
Poco:
public class CurrentSelected:INotifyPropertyChanged
{
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
protected void Notify(string propName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
#endregion
private SolidColorBrush color;
public SolidColorBrush Color
{
get { return color; }
set {
if (this.color==value) return;
this.color = value;
Notify("Color");
}
}
public CurrentSelected() {
this.color = new SolidColorBrush(Colors.Orange);
}
}
主窗口:
<Grid x:Name="LayoutRoot" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="33*"/>
<ColumnDefinition Width="33*"/>
<ColumnDefinition Width="33*"/>
</Grid.ColumnDefinitions>
<local:ColorPicker CurrentColor="{Binding Path=Color, Mode=TwoWay}" Grid.Column="0"/>
<Rectangle Fill="{Binding Path=Color}" Grid.Column="1" Width="160" Height="80" />
<local:PenSelector ColorSelected="{Binding Path=Color, Mode=TwoWay}" Grid.Column="2"/>
</Grid>
代碼背后:
CurrentSelected Settings = new CurrentSelected();
public MainPage()
{
InitializeComponent();
this.DataContext = this.Settings;
}
用戶控件 - ColorPicker
<StackPanel x:Name="LayoutRoot" Orientation="Horizontal">
<Button Click="Button_Click">
<Button.Content>
<Rectangle Fill="Red"/>
</Button.Content>
</Button>
<Button Click="Button_Click">
<Button.Content>
<Rectangle Fill="Green"/>
</Button.Content>
</Button>
<Button Click="Button_Click">
<Button.Content>
<Rectangle Fill="Blue" />
</Button.Content>
</Button>
</StackPanel>
它的代碼:
public static readonly DependencyProperty CurrentColorProperty=
DependencyProperty.Register("CurrentColor",
typeof(SolidColorBrush), typeof(ColorPicker),
new PropertyMetadata(new SolidColorBrush(Colors.Gray)));
public SolidColorBrush CurrentColor
{
get
{
return (SolidColorBrush)GetValue(CurrentColorProperty);
}
private set
{
SetValue(CurrentColorProperty, value);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Button b = (Button)sender;
Shape r = (b.Content) as Shape;
SolidColorBrush sb = new SolidColorBrush(Colors.Yellow);
sb = (SolidColorBrush)r.Fill;
this.CurrentColor = sb;
}
用戶控制PenSelector
xaml就像你只有一個ListBox一樣,所有工作都在后面的代碼中
public static readonly DependencyProperty ColorSelectedProperty =
DependencyProperty.Register(
"ColorSelected",
typeof(SolidColorBrush),
typeof(PenSelector),
new PropertyMetadata(new SolidColorBrush(Colors.Yellow)));
public SolidColorBrush ColorSelected
{
get
{
return (SolidColorBrush)GetValue(ColorSelectedProperty);
}
set
{
SetValue(ColorSelectedProperty, value);
}
}
public PenSelector()
{
InitializeComponent();
LayoutRoot.Items.Add(addRectangle());
LayoutRoot.Items.Add(addRectangle());
}
private Rectangle addRectangle()
{
Rectangle r = new Rectangle() { Width = 160, Height = 80 };
Binding b = new Binding();
b.Source=this;
b.Path=new PropertyPath("ColorSelected");
b.Mode=BindingMode.OneWay;
r.SetBinding(Rectangle.FillProperty, b);
return r;
}
我已經將POCO和DP分別定義為SolidColorBrushes,盡管您可能希望使用Color和轉換器轉換為Brush。 CurrentSelected類實例設置被分配給mainWindows datacontext。 在ColorPicker上,我剛剛將代碼放在一個Button Click處理程序中,並根據xaml中指定的Fill顏色獲取顏色。 這將更新選取器上的CurrentColor DP。 PenSelector設置矩形,並綁定到自己的DP,然后剩下的就是在CurrentSelected公開的Color屬性的MainWindow中設置數據綁定。 DP定義默認值。 還有其他方法可以做到這一切,但這取決於您的要求(一如既往)!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.