mayho33
Goto Top

C-Sharp WPF Controls und Extensions

Hallo @ All!

Ich komme bei einem Problem nicht weiter:

Ich habe ein C# WPF Window mit einem Label und 2 Buttons (Weiter und Zurück). Mit dem Buttons möchte ich das Margin des Labels verändern.

z.B. von:
label_myLabel.Margin = new Thickness(10,20,0,0);

auf

label_myLabel.Margin = new Thickness(50,60,0,0);

Nun dachte ich, es wäre doch einfacher eine Extension dafür zu basteln, welche ich dann einfach so aufrufen könnte:

label_myLabel.newMargin("50,20,0,0");

Leider funkt das nicht so wie ich will. Die Extension wird mir in meiner Methode nicht angezeigt.

Hier der Code der Extension:
public static class Extensions
    {
        public static bool SetMargin(this String newMargin))
        {
            bool toReturn               = false;
            var control                 = new StackTrace().GetFrame(1).GetMethod();
            string cm                 = newMargin.Split(',');  

            try
            {
                if (control.GetType() == typeof(Label))
                    currentLabel.Margin = new Thickness(Convert.ToDouble(cm), Convert.ToDouble(cm[1]), Convert.ToDouble(cm[2]), Convert.ToDouble(cm[3]));
            }
            catch (Exception e)
            {
                toReturn = false;
            }
            
            return toReturn;
        }
    }

Ich bin mir sicher es geht noch was. Nur was könnte das sein? hat jemand einen Tipp?

Danke für die Unterstützung!

Mayho

Content-Key: 315245

Url: https://administrator.de/contentid/315245

Printed on: April 26, 2024 at 22:04 o'clock

Mitglied: 129813
Solution 129813 Sep 14, 2016 updated at 16:01:40 (UTC)
Goto Top
Hi,
you have to inherit from the wpf label class, then you can override or add new methods available on your labels.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/8bcfeb09-aff6-4b2 ...

Regards
Member: atze187
Solution atze187 Sep 15, 2016 updated at 06:20:29 (UTC)
Goto Top
Hi,

entweder leitest Du wie highload schrieb eine Klasse von Label ab oder du gehst den von Dir angedachten Weg der Extension-Methods. Für letzteres sähe der Code wie folgt aus:

public static class Extensions 
{
        public static void SetMargin(this Label label, string margin)
        {
            string cm = margin.Split(',');  
            label.Margin = new Thickness(Convert.ToDouble(cm), Convert.ToDouble(cm[1]), Convert.ToDouble(cm[2]), Convert.ToDouble(cm[3]));
        }
}

Aufgerufen wird das ganze dann so:

label_myLabel.SetMargin("50,20,0,0");  

Durch "this Label label" werden zwei Dinge erreicht:

  • die Methode ist nur mit Labels verwendbar, anderenfalls schmeisst der Compiler einen Fehler
  • "label" ist dann immer das Label für das du die Methode aufgerufen hast, das Suchen und casten des Controls fällt somit weg
Member: mayho33
mayho33 Sep 16, 2016 updated at 22:09:23 (UTC)
Goto Top
Danke atze, thank you higload!

a litle but verry importent detail. after the modification followed the atzes example all works fine face-smile

regards, mayho