微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C#-Silverlight:如何根据当前设置类动态更新属性?

下面是示例的完整代码.我有一个名为ColorPicker的用户控件,其中包含3个按钮,每个按钮显示一种颜色.单击按钮时,将设置CurrentSettings类中的Color属性.我想要发生的是MainPage上矩形的颜色更改为与新的CurrentSettings.Color匹配,第二个用户控件中的列表框中的矩形(在后面的代码添加了)矩形的颜色也更改为与新的CurrentSettings.Color.

我一直在尝试使用Dependency Properties和INotifyPropertyChanged来成功完成此任务,现在决定从头开始.

//当前的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,您处在正确的轨道上.从settings类开始,但是将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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐