Announcement

Collapse
No announcement yet.

WPF - Image bis zur maximalen Grösse des Bildes anpassen

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • WPF - Image bis zur maximalen Grösse des Bildes anpassen

    Hallo Forum
    folgendes habe ich:
    ich habe ein keybinding erstellt, welches mir anhand dessen auswahl ein bild auf den button legt. also wen ich das keybinding auf Cancel setzte kommt ein abbrechen bild und wen ich sage ViewList kommt ein Anzeigen bild.
    dies Funktioniert Prima. Jetzt habe ich das Problem das wen ich den Button standratmässig in den Editor ziehe das er mir den Button überdimensional gross darstellt. so ca 600px auf 600px. keine Ahnung wiso.

    folgendes will ich gerne haben:
    der button soll standardmässig die grösse XY haben beim reinziehen in den Editor. hat das bild die maximale grösse erreicht soll es nicht mehr mitskalliert werden sondern nur noch ausgerichtet werden.

    hier der code des xaml welche den standardbutton überschreibt
    Code:
    <Grid x:Name="LayoutRoot" MinWidth="120" Width="auto" MinHeight="22" Height="auto" Margin="0" VerticalAlignment="Center"  Background="{TemplateBinding Background}">
    <!-- Button Content -->
                            <StackPanel HorizontalAlignment="Center" Margin="0" Grid.RowSpan="1" Orientation="Horizontal">
                                <Image Source="{Binding Path=Image, RelativeSource={RelativeSource TemplatedParent}}" 
                                    Width="auto" Height="auto" Stretch="UniformToFill" Margin="10,0,0,0" HorizontalAlignment="Center"/>
                                <TextBlock Text="{TemplateBinding Text}" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Segoe UI" FontWeight="Bold" Margin="6,0,0,0" 
                                    Foreground="{TemplateBinding Background, Converter={StaticResource ColorConverter}, ConverterParameter='0.5'}" />
                            </StackPanel>
    </Grid>
    der geschriebene code für
    Code:
    namespace Apra.iQOS.WpfCommon
    {
    
        public class iQOSImageButton : Button
        {
            #region Fields
    
            // Dependency property backing variables
            public static readonly DependencyProperty ImageProperty;
            public static readonly DependencyProperty TextProperty;
    
            #endregion
    
            #region Constructors
    
            /// <summary>
            /// Default constructor.
            /// </summary>
            static iQOSImageButton()
            {
                iQOSWpfUIHelper.DebugTrace("iQOSImageButton::Ctor(Static)");
                
                // Initialize as lookless control
                DefaultStyleKeyProperty.OverrideMetadata(typeof(iQOSImageButton), new FrameworkPropertyMetadata(typeof(iQOSImageButton)));
    
                // Initialize dependency properties
                ImageProperty = DependencyProperty.Register("Image", typeof(ImageSource), typeof(iQOSImageButton), new UIPropertyMetadata(null));                                
                TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(iQOSImageButton), new UIPropertyMetadata(null));
            }
    
    
            /// <summary>
            /// Initializes a new instance of the <see cref="iQOSImageButton"/> class.
            /// </summary>
            public iQOSImageButton()
            {
                iQOSWpfUIHelper.DebugTrace("iQOSImageButton::Ctor()");     
                this.Loaded += iQOSTaskButton_Loaded;       
            }
    
            #endregion
    
            /// <summary>
            /// Handles the Loaded event of the iQOSImageButton control.
            /// </summary>
            /// <param name="sender">The source of the event.</param>
            /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
            private void iQOSTaskButton_Loaded(object sender, RoutedEventArgs e)
            {
                iQOSWpfUIHelper.DebugTrace("iQOSImageButton::iQOSTaskButton_Loaded" +
                 " Name = [" + Name + "]" +
                 " TextId = [" + TextId + "]");
    
                if (Background == null)
                {
                    Background = Brushes.LimeGreen;
                }
    
                if (iQOSWpfUIHelper.IsInDesignMode() == false)
                {
                    // Add assigned key to window keylist
                    if (KeyBinding != iQOSAction.EKeyBinding.Undefined)
                    {
                        //Apra.iQOS.WpfCommon.iQOSWindow.KeyAssignmentCollection.Add(KeyBinding, new iQOSAction.KeyAssignment(KeyBinding, this));
                    }
                }
    
                if (iQOSWpfUIHelper.IsInDesignMode() == false)
                {
                    // Get text from wrapper
                    if (TextId != 0)
                    {
                        WrapperAccess.TextObject textObject = WrapperAccess.GetTextById(TextId, null);
                        Text = textObject.Text;
                    }
                }
                LoadImage();
            }
    
            /// <summary>
            /// Loads the image.
            /// </summary>
            private void LoadImage()
            {
                string imageName = "";
                // Load image if not defined in resource based on KEYBINDING
                // Otherwise use XAML definition of Image
                if (Image == null)
                {
                    if (KeyBinding != iQOSAction.EKeyBinding.Undefined)
                    {
                        var action = (iQOSAction)iQOSAction.ActionMap[KeyBinding];
    
                        if (action != null && action.ImageName != null)
                        {
                            imageName = action.ImageName;
                            Text = action.LabelGerman;
                        }
                    }
                    if (String.IsNullOrEmpty(imageName) == false)
                    {
                        iQOSWpfUIHelper.DebugTrace("iQOSImageButton::LoadImage() Load Image = [" + imageName + "]");
                        BitmapImage image = new BitmapImage();
                        image.BeginInit();
                        try
                        {
                            image.UriSource = new Uri("pack://application:,,/iQOSWpfCommon;Component/Images/" + imageName);
                            Image = image;
                        }
                        catch (Exception ex)
                        {
                            ;
                        }
                        image.EndInit();
                    }
                }
                else
                {
                    iQOSWpfUIHelper.DebugTrace("iQOSImageButton::LoadImage() USE XAML");
                }
            }
    
            // ---------------------------------------------------------------
            // Custom dependency property Image
            // ---------------------------------------------------------------
            [Description("Image"), Category("iQOS Properties")]
            public ImageSource Image
            {
                get { return (ImageSource)GetValue(ImageProperty); }
                set { SetValue(ImageProperty,  value); }
            }
    
            // ---------------------------------------------------------------
            // Custom dependency property Image
            // ---------------------------------------------------------------
            [Description("Text"), Category("iQOS Properties")]
            public string Text
            {
                get { return (string)GetValue(TextProperty); }
                set { SetValue(TextProperty, value); }
            }
    
            // ---------------------------------------------------------------
            // Custom property KeyBinding
            // ---------------------------------------------------------------
            private iQOSAction.EKeyBinding _keyBinding;
            [Category("iQOS Properties")]
            [Browsable(true)]
            [EditorAttribute(typeof(iQOSWpfUIHelper.DropDownUIEditor),
            typeof(System.Drawing.Design.UITypeEditor))]
            public iQOSAction.EKeyBinding KeyBinding
            {
                get
                {
                    return this._keyBinding;
                }
    
                set
                {
                    this._keyBinding = value;
                    iQOSAction action = (iQOSAction)iQOSAction.ActionMap[(iQOSAction.EKeyBinding)value];
                    
                    if (iQOSWpfUIHelper.IsInDesignMode() == true)
                    {
                        Content = action.LabelGerman;
                        TextId = action.TextId;
                    }
                    else
                    {
                        if (TextId != 0)
                        {
                            WrapperAccess.TextObject textObject = WrapperAccess.GetTextById(TextId, null);
                            Content = textObject.Text;
                        }
                    else
                        {
                            WrapperAccess.TextObject textObject = WrapperAccess.GetTextById(action.TextId, null);
                            Content = textObject.Text;
                        }
                    }
                    // Invalidate image
                    Image = null;
                    LoadImage();
    
                }
            }
    
            // ---------------------------------------------------------------
            // Custom property TextId
            // ---------------------------------------------------------------
            int _TextId;
            [Description("Text Id"), Category("iQOS Properties")]
            public int TextId
            {
                get 
                {
                    return _TextId;
                }
                set 
                {
                    _TextId = value;
                } 
            }
        }
    }
    ich wäre froh wen mir jemand helfen kann oder tipps geben kann. zusagen gilt es, dass ich nicht aller code selber geschrieben habe sondern von einem anderen entwickler.
Working...
X