UWP-如何在集合中添加颜色

时间:2018-05-30 16:11:25

标签: c# wpf uwp

我正试着用颜色填充我的收藏品。我有一个边框控件,用于显示使用PrepareContainerForItemOverride选择的颜色 方法,我有一个按钮控件来弹出集合中的颜色。这是我的代码。

Picker.cs

public sealed class Picker : ItemsControl
{
    private ObservableCollection<SolidColorBrush> _myColors;
    public Picker()
    {
        this.DefaultStyleKey = typeof(Picker);
       _myColors = new ObservableCollection<SolidColorBrush>()
     {
        new SolidColorBrush(Color.FromArgb(255,225,225,25)),
        new SolidColorBrush(Color.FromArgb(255,225,25,25)),
        new SolidColorBrush(Color.FromArgb(255,225,225,225)),
        new SolidColorBrush(Color.FromArgb(255,25,225,25))
     };

    }

    public ObservableCollection<SolidColorBrush> MyColors
    {
        get
        {
            return (ObservableCollection<SolidColorBrush>)GetValue(MyColorsProperty);
        }
        set { SetValue(MyColorsProperty, value); }
    }

    public static readonly DependencyProperty MyColorsProperty =
        DependencyProperty.Register("MyColors",
     typeof(ObservableCollection<SolidColorBrush>), typeof(Picker), new
         PropertyMetadata(null));


    public Popup popup;
    protected override void OnApplyTemplate()
    {
        popup = GetTemplateChild("myPopup") as Popup;
        var popupbutton = GetTemplateChild("btn1") as Button;
        popupbutton.Click += Popupbutton_Click;
    }

    private void Popupbutton_Click(object sender, RoutedEventArgs e)
    {
        popup.IsOpen = popup.IsOpen ? false : true;
    }

    public bool openPopup
    {
        get { return (bool)GetValue(openPopupProperty); }
        set { SetValue(openPopupProperty, value); }
    }
    public static readonly DependencyProperty openPopupProperty =
        DependencyProperty.Register("openPopup", typeof(bool),
   typeof(Picker), new PropertyMetadata(true));


    public object Data { get; private set; }

    public event PropertyChangedEventHandler PropertyChanged;


    public void OnPropertyChanged(String propname)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propname));
        }
    }
}

Generic.xaml

<Style TargetType="local:Picker" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:Picker">
                <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <StackPanel>
                            <Border Height="50" BorderBrush="Blue" BorderThickness="5">
                                <Border.Background>
                                    <SolidColorBrush Color="{Binding Color}" />
                                </Border.Background>
                            </Border>
                            <Popup Name="myPopup" IsOpen="False" IsLightDismissEnabled="True">
                                <Grid Width="650" Height="300" Background="Red">
                                    <ItemsControl ItemsSource="{TemplateBinding MyColors}">
                                        <ItemsControl.ItemTemplate>
                                            <DataTemplate>
                                                <Grid>
                                                    <Border BorderThickness="6" BorderBrush="Black">
                                                        <Border.Background>
                                                            <SolidColorBrush Color="{Binding Color}" />
                                                        </Border.Background>
                                                    </Border>
                                                </Grid>
                                            </DataTemplate>
                                        </ItemsControl.ItemTemplate>
                                    </ItemsControl>
                                </Grid>
                            </Popup>
                        </StackPanel>
                        <Button Grid.Column="1" Name="btn1" Height="55" Content="Click me"/>
                    </Grid>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

在这里,单击按钮时我无法弹出集合中的颜色,我需要使用PrepareContainerForItemOverride方法在边框控制中显示所选颜色

1 个答案:

答案 0 :(得分:0)

问题是你对ItemsSource绑定了错误的属性。你从未使用的私人领域_myColors。我编辑了你的代码。它适用于我。

<强> Picker.cs

 public Picker()
 {
     this.DefaultStyleKey = typeof(Picker);
     MyColors = new ObservableCollection<SolidColorBrush>()
  {
     new SolidColorBrush(Color.FromArgb(255,225,225,25)),
     new SolidColorBrush(Color.FromArgb(255,225,25,25)),
     new SolidColorBrush(Color.FromArgb(255,225,225,225)),
     new SolidColorBrush(Color.FromArgb(255,25,225,25))
  };

 }

 public ObservableCollection<SolidColorBrush> MyColors
 {
     get
     {
         return (ObservableCollection<SolidColorBrush>)GetValue(MyColorsProperty);
     }
     set { SetValue(MyColorsProperty, value); }
 }

 public static readonly DependencyProperty MyColorsProperty =
     DependencyProperty.Register("MyColors",
  typeof(ObservableCollection<SolidColorBrush>), typeof(Picker), new
      PropertyMetadata(null));

<强> Generic.xaml

<Style TargetType="local:Picker" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:Picker">
                <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <StackPanel>
                            <Border Height="50" BorderBrush="Blue" BorderThickness="5">
                                <Border.Background>
                                    <SolidColorBrush Color="{Binding Color}" />
                                </Border.Background>
                            </Border>
                            <Popup Name="myPopup" IsOpen="False" IsLightDismissEnabled="True">
                                <Grid Width="650" Height="300" >
                                    <ItemsControl ItemsSource="{TemplateBinding MyColors}">
                                        <ItemsControl.ItemTemplate>
                                            <DataTemplate>
                                                <Grid>
                                                    <Border BorderThickness="15" BorderBrush="{Binding}">
                                                    </Border>
                                                </Grid>
                                            </DataTemplate>
                                        </ItemsControl.ItemTemplate>
                                    </ItemsControl>
                                </Grid>
                            </Popup>
                        </StackPanel>
                        <Button Grid.Column="1" Name="btn1" Height="55" Content="Click me"/>
                    </Grid>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

enter image description here