none
wpf my colors are not changing RRS feed

  • Question

  • So I am trying to set my Ellipse to gray when the program load and then to green when someone click on it, but it is not working.

    when it loads, I can not see my ellipses at all and when I click on them, my message box does come up, but they do not change colors.

    here is mt code:

     private void WaferLoaded(object sender, RoutedEventArgs e)
            {
                var eColors = new List<MyColor>(14);
                ellipseColors = new ObservableCollection<MyColor>(eColors);
    
                for (int i = 0; i < 13; i++)
                {
                  //  ellipseColors[i].color = Brushes.Gray;
                    ellipseColors.Add(new MyColor { color = Brushes.Gray });
                }
         
            }
    
            
    
            private void Ellipse_MouseDown(object sender, MouseButtonEventArgs e)
            {
                Ellipse EP = sender as Ellipse;
                ellipseColors[5].color = Brushes.Green;
                _waferLocation = EP.Name;
                this.OnEllipseClick?.Invoke();
                OnWaferEllipseChanged(EventArgs.Empty);
            }
    
    
        class MyColor : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            private SolidColorBrush _color;
    
            public SolidColorBrush color
            {
                get
                {
                    return _color;
                }
    
                set
                {
                    _color = value;
                    OnPropertyChanged("color");
                }
            }
    
            void OnPropertyChanged(string propertyName)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    
    
    
     <Viewbox>
                <Grid x:Name="MainGrid">
                    <Ellipse StrokeThickness="3" Width="500" Height="500"  Stroke="Black" />
                    <Ellipse x:Name="Spot5"  StrokeThickness="6" Width="40" Height="40" Margin="219,211,241,249" MouseDown="Ellipse_MouseDown">
                        <Ellipse.Fill>
                            <SolidColorBrush Color = "{Binding Path=ellipseColors[0].color, IsAsync=True}"/>
                            </Ellipse.Fill>
                    </Ellipse>
                    
                    <Ellipse x:Name="Spot6"  StrokeThickness="6" Width="40" Height="40" Margin="25,270,435,190"  MouseDown="Ellipse_MouseDown">
                    <Ellipse.Fill>
                            <SolidColorBrush Color = "{Binding Path=  ellipseColors[1].color, IsAsync=True}"/>
                    </Ellipse.Fill>
                    </Ellipse>
    
    ...
    
    

    can someone tell me what I am doing wrong ?

    Monday, September 16, 2019 9:46 PM

All replies

  • Hi Btb4198,

    >I can not see my ellipses at all

    You seems missing some code for your data binding source, I add some part for your code for showing the ellipse as the following picture:

    <Window.Resources>

            <local:MainViewModel x:Key="myEllipseColors"></local:MainViewModel>

    </Window.Resources>

    <Ellipse x:Name="Spot5" Fill="{Binding Path=ellipseColors[1].color, Source={StaticResource myEllipseColors}}" StrokeThickness="6" Width="40" Height="40" Margin="20,20,20,20 " MouseDown="Ellipse_MouseDown">

    </Ellipse>

    The following is the code for MainViewModel

    public class MainViewModel

        {

            public ObservableCollection<MyColor> ellipseColors { get; protected set; }

            public MyColor myColor { get; set; }

            public MainViewModel()

            {

                WaferLoaded();

            }

            public ObservableCollection<MyColor> WaferLoaded()

            {

                var eColors = new List<MyColor>(14);

                ellipseColors = new ObservableCollection<MyColor>(eColors);

                for (int i = 0; i < 13; i++)

                {

                    //  ellipseColors[i].color = Brushes.Gray;

                    ellipseColors.Add(new MyColor { color = Brushes.Gray });

                }

                return ellipseColors;

            }

    }

    Best regards

    Daisy Tian


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 18, 2019 3:09 AM