2016-07-05 13 views
0

Ich möchte Eigenschaft aus dem DataContext des Hauptfensters binden, oberhalb können Sie meine UserControls und Modelle sehen. Daher möchte ich die Eigenschaften Model.ID.Label1 und Model.ID.Label2 an die Steuerelemente main_id/card_1/top und main_id/card_1/bottom binden. Ich hoffe es ist klar. Wenn ich das ref_lbl Label aktiviere, zeigt es das "lbl1" an, das card_2, das noch mit den hardcoded Texten arbeitet, aber das card_1 ist leer. Was muss ich ändern, um die Bindung für card_1 zu beheben?WPF-Datenbindung über UserControls

Ich habe eine ID UserControl und es enthält ein anderes UserControl.

XAML:

<UserControl x:Class="stack.ID" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:Controls="clr-namespace:stack.Controls" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="1*" /> 
     <RowDefinition Height="1*" /> 
    </Grid.RowDefinitions> 
    <Label Name="ref_lbl" Grid.Row="0" Content="{Binding Label1}" Visibility="Collapsed" /> 
    <Controls:Card x:Name="card_1" Grid.Row="0" TopLabel="{Binding Label1}" BottomLabel="{Binding Label2}" /> 
    <Controls:Card x:Name="card_2" Grid.Row="1" TopLabel="Text 1" BottomLabel="Text 2" /> 
</Grid> 

-Code Behind: standardmäßig automatisch generiert

Hier ist die Karte Usercontrol.

XAML:

<UserControl x:Class="stack.Controls.Card" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300" DataContext="{Binding RelativeSource={RelativeSource Self}}"> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="1*" /> 
     <RowDefinition Height="1*" /> 
    </Grid.RowDefinitions> 
    <Label Grid.Row="0" FontSize="20" Foreground="Black" HorizontalAlignment="Center" VerticalAlignment="Center" Content="{Binding TopLabel}" /> 
    <Label Grid.Row="1" FontSize="20" Foreground="Black" HorizontalAlignment="Center" VerticalAlignment="Center" Content="{Binding BottomLabel}" /> 
</Grid> 

-Code Behind:

namespace stack.Controls 
{ 
    public partial class Card : UserControl 
    { 
     public static readonly DependencyProperty TopLabelProperty = DependencyProperty.Register("TopLabel", typeof(string), typeof(Card), new PropertyMetadata(default(string))); 
     public static readonly DependencyProperty BottomLabelProperty = DependencyProperty.Register("BottomLabel", typeof(string), typeof(Card), new PropertyMetadata(default(string))); 
     public Card() 
     { 
      InitializeComponent(); 
     } 
     public string TopLabel 
     { 
      get 
      { 
       return (string)GetValue(TopLabelProperty); 
      } 
      set 
      { 
       SetValue(TopLabelProperty, value); 
      } 
     } 
     public string BottomLabel 
     { 
      get 
      { 
       return (string)GetValue(BottomLabelProperty); 
      } 
      set 
      { 
       SetValue(BottomLabelProperty, value); 
      } 
     } 
    } 
} 

Und hier ist mein Hauptfenster.

XAML:

<Window x:Class="stack.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:stack" 
    Title="MainWindow" Height="350" Width="525"> 
<Window.DataContext> 
    <local:Model /> 
</Window.DataContext> 
<Grid> 
    <local:ID x:Name="main_id" DataContext="{Binding ID}" /> 
</Grid> 

-Code Behind: standardmäßig automatisch generiert

Und ich habe auch zwei Modelle. Entfernen

namespace stack 
{ 
    public class IDModel 
    { 
     private string label1 = "lbl1"; 
     private string label2 = "lbl2"; 
     public string Label1 
     { 
      get 
      { 
       return label1; 
      } 
      set 
      { 
       label1 = value; 
      } 
     } 
     public string Label2 
     { 
      get 
      { 
       return label2; 
      } 
      set 
      { 
       label2 = value; 
      } 
     } 
    } 
    public class Model 
    { 
     private IDModel id = new IDModel(); 
     public IDModel ID 
     { 
      get 
      { 
       return id; 
      } 
      set 
      { 
       id = value; 
      } 
     } 
    } 
} 

Antwort

1

DataContext="{Binding RelativeSource={RelativeSource Self}}" 

aus der Card ‚s XAML.

Es verhindert, dass die Datacontext von seiner Mutter ID Kontrolle erben, die notwendig ist, wenn man

<Controls:Card ... TopLabel="{Binding Label1}" /> 

Stattdessen schreiben die Content Bindungen in Card ‚s XAML wie folgt schreiben:

<Label ... Content="{Binding TopLabel, 
    RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" /> 
+0

Danke für Ihre schnelle Antwort, es hat es gelöst! –

+0

Wenn Sie den DataContext vom übergeordneten Element erben, müssen Sie die RelativeSource noch festlegen? – Funk

+1

Ja, Sie müssen RelativeSource einstellen, sonst funktioniert es nicht. –