- 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 DPs SolidColorBrushes, , , . CurrentSelected datacontext.
ColorPicker , , , xaml. CurrentColor DP Picker. PenSelector Rectangles DP, MainWindow Color, CurrentSelected. DP . , ( )!