Translate ImageButton from C# to XAML

Posted by Bill on Stack Overflow See other posts from Stack Overflow or by Bill
Published on 2010-03-28T21:09:02Z Indexed on 2010/03/28 21:13 UTC
Read the original article Hit count: 400

Filed under:
|
|

I worked out the C# code to create an ImageButton (below) that has three images (one base-image and two overlays) and three text boxes as the face of the button. I am inheriting from the Button class, which unfortunately includes several components that I didn't realize would surface until after coding and need to remove, namely the bright-blue surrounding border on IsMouseOver, and any visible borders between the buttons, as the buttons will end up in a wrapPanel and the borders need to be seamless. Now that the format has been worked out in C#, I expect that I need to translate to XAML so that I can create a ControlTemplate to get the functionality necessary, however I am not certain as to the process of translating from C# to XAML. Can anyone steer me in the right direction?

public class ACover : Button
    {
        Image cAImage = null;
        Image jCImage = null;
        Image jCImageOverlay = null;
        TextBlock ATextBlock = null;
        TextBlock AbTextBlock = null;
        TextBlock ReleaseDateTextBlock = null;
        private string _TracksXML = "";

        public ACover() 
        {
            Grid cArtGrid = new Grid();

            cArtGrid.Background = new SolidColorBrush(Color.FromRgb(38, 44, 64));
            cArtGrid.Margin = new System.Windows.Thickness(5, 10, 5, 10);
            RowDefinition row1 = new RowDefinition();
            row1.Height = new GridLength(225); 
            RowDefinition row2 = new RowDefinition();
            row2.Height = new GridLength(0, GridUnitType.Auto);
            RowDefinition row3 = new RowDefinition();
            row3.Height = new GridLength(0, GridUnitType.Auto);
            RowDefinition row4 = new RowDefinition();
            row4.Height = new GridLength(0, GridUnitType.Auto);
            cArtGrid.RowDefinitions.Add(row1);
            cArtGrid.RowDefinitions.Add(row2);
            cArtGrid.RowDefinitions.Add(row3);
            cArtGrid.RowDefinitions.Add(row4);

            ColumnDefinition col1 = new ColumnDefinition();
            col1.Width = new GridLength(0, GridUnitType.Auto);
            cArtGrid.ColumnDefinitions.Add(col1);

            jCImage = new Image();
            jCImage.Height = 240;
            jCImage.Width = 260;
            jCImage.VerticalAlignment = VerticalAlignment.Top;
            jCImage.Source = new BitmapImage(new Uri(Properties.Settings.Default.pathToGridImages + "jc.png", UriKind.Absolute));
            cArtGrid.Children.Add(jCImage);

            cArtImage = new Image();
            cArtImage.Height = 192;
            cArtImage.Width = 192;
            cArtImage.Margin = new System.Windows.Thickness(3, 7, 0, 0);
            cArtImage.VerticalAlignment = VerticalAlignment.Top;
            cArtGrid.Children.Add(cArtImage);

            jCImageOverlay = new Image();
            jCImageOverlay.Height = 192;
            jCImageOverlay.Width = 192;
            jCImageOverlay.Margin = new System.Windows.Thickness(3, 7, 0, 0);
            jCImageOverlay.VerticalAlignment = VerticalAlignment.Top;
            jCImageOverlay.Source = new BitmapImage(new Uri( Properties.Settings.Default.pathToGridImages + "jc-overlay.png", UriKind.Absolute));
            coverArtGrid.Children.Add(jCImageOverlay);

            ATextBlock = new TextBlock();
            ATextBlock.Foreground = new SolidColorBrush(Color.FromRgb(173, 176, 198));
            ATextBlock.Margin = new Thickness(10, -10, 0, 0);
            cArtGrid.Children.Add(ATextBlock);

            AlTextBlock = new TextBlock();
            AlTextBlock.Margin = new Thickness(10, 0, 0, 0);
            AlTextBlock.Foreground = new SolidColorBrush(Color.FromRgb(173, 176, 198));
            cArtGrid.Children.Add(AlTextBlock);

            RDTextBlock = new TextBlock();
            RDTextBlock.Margin = new Thickness(10, 0, 0, 0);
            RDTextBlock.Foreground = new SolidColorBrush(Color.FromRgb(173, 176, 198));
            cArtGrid.Children.Add(RDTextBlock);

            Grid.SetColumn(jCImage, 0);
            Grid.SetRow(jCImage, 0);
            Grid.SetColumn(jCImageOverlay, 0);
            Grid.SetRow(jCImageOverlay, 0);
            Grid.SetColumn(cArtImage, 0);
            Grid.SetRow(cArtImage, 0);

            Grid.SetColumn(ATextBlock, 0);
            Grid.SetRow(ATextBlock, 1);
            Grid.SetColumn(AlTextBlock, 0);
            Grid.SetRow(AlTextBlock, 2);
            Grid.SetColumn(RDTextBlock, 0);
            Grid.SetRow(RDTextBlock, 3);
            this.Content = cArtGrid;
        }

        public string A
        {
            get { if (ATextBlock != null) return ATextBlock.Text;
                else return String.Empty; }
            set { if (ATextBlock != null) ATextBlock.Text = value; }
        }

        public string Al
        {
            get { if (AlTextBlock != null) return AlTextBlock.Text;
                  else return String.Empty; }
            set { if (AlTextBlock != null) AlTextBlock.Text = value; }
        }

        public string RD
        {
            get { if (RDTextBlock != null) return RDTextBlock.Text;
                  else return String.Empty; }
            set { if (RDTextBlock != null) RDTextBlock.Text = value; }
        }

        public ImageSource Image
        {
            get { if (cArtImage != null) return cArtImage.Source;
                  else return null; }
            set { if (cArtImage != null) cArtImage.Source = value; }
        }

        public string TracksXML
        {
            get { return _TracksXML; }
            set { _TracksXML = value; }
        }

        public double ImageWidth
        {
            get { if (cArtImage != null) return cArtImage.Width;
                  else return double.NaN;  }
            set { if (cArtImage != null) cArtImage.Width = value; }
        }

        public double ImageHeight
        {
            get { if (cArtImage != null) return cArtImage.Height;
                  else return double.NaN;  }
            set { if (cArtImage != null) cArtImage.Height = value; }
        }
    }

© Stack Overflow or respective owner

Related posts about imagebutton

Related posts about wpf