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.

Empfohlene Antworten

Veröffentlicht

Ich habe ein UserControl mit einem Label das an eine DependencyProperty in der CodeBehind Datei gebunden ist


<Label Content="{Binding Path=MyText}" Foreground="Black"/>
[/PHP]

[PHP]private static readonly DependencyProperty MyTextProperty =
DependencyProperty.Register("MyText", typeof(string), typeof(MyUserControl), new FrameworkPropertyMetadata(new PropertyChangedCallback(MyTextPropertyChanged)));

public string MyText
{
get
{
return (string)GetValue(MyTextProperty);
}
set
{
SetValue(MyTextProperty, value);
}
}



private static void MyTextPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
}

public MyUserControl()
{
DataContext = this;
InitializeComponent();
MyText = "Hallo";
}

Binde ich das UserControl jetzt in einer anderen XAML View ein ohne das ich MyText setzte gibt das ganz normal Hallo aus.

Binde ich aber MyText an einen Wert aus dem View Model gibt das plötzlich nichts mehr aus.

<Label Content="{Binding Path=MyOriginalText}"/>
<iv:MyUserControl MyText="{Binding Path=MyOriginalText}"/>[/PHP]

Das erste Label gibt aber den richtigen Text aus, es ist also kein Problem des Bindings in der View sondern muss irgendwo an dem UserControl bzw. dem DependencyProperty liegen.

Wenn ich einen Haltepunkt in die MyTextPropertyChanged Methode setzte dann hält der da zwei mal an. Beim ersten Mal mit dem alten Wert null und dem neuen Wert "Hallo" und beim zweiten Mal mit dem alten Wert "Hallo" und dem neuen Wert null:confused:

Irgendwelche Ideen warum das nicht klappt?

  • Autor

Ok habs selber gelöst. Ein Blick ins Output Fenster hat mir gezeigt das er durch das

DataContext = this;

bereits hier:

<iv:MyUserControl MyText="{Binding Path=MyOriginalText}"/>  

MyOriginalText im UserControl selber gesucht hat und nicht mehr im DataContext der View die das einbindet.

Ich hab das jetzt entfernt und im Binding innerhalb des UserControls noch

RelativeSource={RelativeSource AncestorType={x:Type UserControl}}

mit angegeben.

Gefällt mir zwar nicht so gut aber die einzige Alternative die ich sehe wäre ein ViewModel für das UserControl zu erstellen was ich aber nicht möchte.

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.