有没有一种方法可以使用对基础ItemsControl.ItemSource的更改来更改XAML中的矩形颜色?

时间:2019-02-02 15:13:57

标签: c# wpf xaml

在示例代码创建三个矩形一个按钮被点击时。当用户点击触发MouseDown事件任何矩形和矩形的颜色被选择的颜色和非选择色状态之间切换。

目前可以在MouseDown事件我通过在实施例的最后一行直接使用C#Rectangle.Fill改变点击矩形的颜色可见。

有一种方法使用改变底层数据源来改变在XAML中的矩形彩色ItemsControl的命名CustomButtonList吗

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:system="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid Margin="0,0,57.6,-0.4">
        <Button Content="Button" HorizontalAlignment="Left" Margin="29,233,0,0" VerticalAlignment="Top" Width="110" Click="Button_Click"/>
        <ItemsControl x:Name="CustomButtonList" ItemsSource="{Binding CustomButtonItem}" Margin="10,0,58,0" RenderTransformOrigin="-0.047,0.479">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemContainerStyle>
                <Style TargetType="ContentPresenter">
                    <Setter Property="Canvas.Left" Value="{Binding X}"/>
                    <Setter Property="Canvas.Top" Value="{Binding Y}"/>
                </Style>
            </ItemsControl.ItemContainerStyle>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Rectangle Width="{Binding Width}" Height="{Binding Height}" Fill="{Binding FaceColor}" MouseDown="Buttons_MouseDown" Tag="{Binding Tag}" Opacity="{Binding FaceOpacity}" Stroke="{Binding StrokeColor}" StrokeThickness="{Binding StrokeThickness}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Window>

C#代码:

using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        List<CustomButtonItem> items;

        public MainWindow()
        {
            InitializeComponent();
            items = new List<CustomButtonItem>();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {            
            items.Add(new CustomButtonItem() { X = 20, Y = 20, Height = 50, Width = 100, Tag = "1", NotSelectedFaceColor = new SolidColorBrush(Color.FromRgb(0, 111, 111)), SelectedFaceColor = new SolidColorBrush(Color.FromRgb(255, 0, 0)), FaceOpacity = 1.0, StrokeColor = new SolidColorBrush(Color.FromRgb(255, 0, 0)), StrokeThickness = 2 });
            items.Add(new CustomButtonItem() { X = 125, Y = 20, Height = 50, Width = 100, Tag = "2", NotSelectedFaceColor = new SolidColorBrush(Color.FromRgb(0, 111, 111)), SelectedFaceColor = new SolidColorBrush(Color.FromRgb(255, 0, 0)), FaceOpacity = 1.0, StrokeColor = new SolidColorBrush(Color.FromRgb(255, 0, 0)), StrokeThickness = 2 });
            items.Add(new CustomButtonItem() { X = 230, Y = 20, Height = 50, Width = 100, Tag = "3", NotSelectedFaceColor = new SolidColorBrush(Color.FromRgb(0, 111, 111)), SelectedFaceColor = new SolidColorBrush(Color.FromRgb(255, 0, 0)), FaceOpacity = 0.1, StrokeColor = new SolidColorBrush(Color.FromRgb(255, 0, 0)), StrokeThickness = 2 });

            for (int i = 0; i < items.Count; i++)
            {
                items[i].UpdateButtonState();
            }

            CustomButtonList.ItemsSource = items;
        }

        public class CustomButtonItem
        {
            public double X { get; set; }
            public double Y { get; set; }
            public double Width { get; set; }
            public double Height { get; set; }
            public string Tag { get; set; }
            public double FaceOpacity { get; set; }
            public SolidColorBrush StrokeColor { get; set; }
            public double StrokeThickness { get; set; }
            public SolidColorBrush NotSelectedFaceColor { get; set; }
            public SolidColorBrush SelectedFaceColor { get; set; }
            private bool Selected { get; set; }
            public SolidColorBrush FaceColor { get; set; }

            public void UpdateButtonState()
            {
                if (Selected == true)
                {
                    FaceColor = SelectedFaceColor;                    
                }
                else
                {
                    FaceColor = NotSelectedFaceColor;                    
                }
            }

            public void ToggleSelection()
            {
                if (Selected == true)
                {
                    Selected = false;
                }
                else
                {
                    Selected = true;
                }
                UpdateButtonState();
            }

        }

        public class ViewModel
        {
            public ObservableCollection<CustomButtonItem> RectItems { get; set; }
        }

        private void Buttons_MouseDown(object sender, MouseButtonEventArgs e)
        {

            Rectangle mClickedObject = e.Source as Rectangle;
            //MessageBox.Show("Mousedown on object : " + mClickedObject.Tag);

            for (int i = 0; i < CustomButtonList.Items.Count; i++)
            {
                if (items[i].Tag == mClickedObject.Tag.ToString())
                {
                    items[i].ToggleSelection();  
                    mClickedObject.Fill = items[i].FaceColor;  // can this line be avoided by using XAML somehow??
                }
            }
        }
    }
}

更新 我尝试以下了。制作的ItemSource = null,并且赋予它似乎再次做工精细,但这似乎有点笨拙,所以问题仍然站立有没有更好的办法?

private void Buttons_MouseDown(object sender, MouseButtonEventArgs e)
{

    Rectangle mClickedObject = e.Source as Rectangle;

    for (int i = 0; i < CustomButtonList.Items.Count; i++)
    {
        if (items[i].Tag == mClickedObject.Tag.ToString())
        {
            items[i].ToggleSelection();
            CustomButtonList.ItemsSource = null;
            CustomButtonList.ItemsSource = items;
        }
    }
}

}

0 个答案:

没有答案