Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[WPF] DependencyProperty in UserControl OnRender() verwenden

Empfohlene Antworten

Veröffentlicht

Hallo ihr,

ich möchte in einem UserControl eine Eigenschaft haben, an die ich per Binding Daten binden kann.

Das funktioniert auch soweit, indem ich eine DependencyProperty erstelle. Aber wie kann ich in dem Control dann bei Änderung der Eigenschaft z.B. InvalidateVisual aufrufen oder irgendwas neu berechnen lassen?

OnPropertyChanged hilft mir da nicht weiter, da dies für DependencyProperties static sein muss, oder?

Wie ist denn dazu der richtige Ansatz?

  • Autor
Soll "InvalidateVisual" aufgerufen werden, wenn sich dein DP ändert oder soll sich dein DP neu berechnen, wenn sich "InvalidateVisual" ändert?

Es soll erst eine Methode namens Calculate() und anschließend InvalidateVisual() aufgerufen werden, wenn sich die DP ändert.

Beispiel: DP heißt RectangleCount;

Dies setze ich per Binding (<myControl RectangleCount="{Binding ViewModel.Count}" /> auf einen Wert und immer wenn dieser Wert geändert wird, möchte ich in OnRender() neue Rechtecke usw. zeichnen.

Eventuell so?


        public static readonly DependencyProperty RectangleCountProperty =

            DependencyProperty.Register(

                "RectangleCount",

                typeof(int),

                typeof(UserControl1),

                new PropertyMetadata(OnDependencyPropertyChanged));

.

.

.

        private static void OnDependencyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

        {

            var sender = (UserControl1)d;


             Calculate();


            sender.InvalidateVisual();


            //var oldValue = e.OldValue;

            //var newValue = e.NewValue;

        }

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.