2015-06-18 11 views
7

Próbuję powiązać właściwość, która znajduje się poza obiektem Itemscontrol. Jednak to nie działa.Powiąż właściwość, która znajduje się poza obiektem Itemscontrol w XAML

Wygląda na to, że w ItemControl DataTemplate odnosi się do tego, co znajduje się wewnątrz kolekcji, a nie poza nią. Próbowałem z RelativeResource i odwołałem do AncestorType dla ViewModel.

Code (VM):

public class Test { 
    public string GetThis {get{return "123";} set{}} 
    public List<string> IterateProperty {get; set;} 
} 

XAML (Widok):

<ItemsControl ItemsSource="{Binding Path=IterateProperty}"> 
    <ItemsControl.ItemTemplate> 
    <DataTemplate> 
     <TextBlock Text="I want to bind the string property GetThis!" /> 
+0

Czy możesz sprawdzić mój przykład, jak zdefiniować swoje właściwości? To może być pomocne. –

Odpowiedz

8

trzeba wiązać się DataContext rodzica ItemsControl.

<ItemsControl ItemsSource="{Binding Path=IterateProperty}"> 
    <ItemsControl.ItemTemplate> 
    <DataTemplate> 
     <TextBlock Text="{Binding DataContext.GetThis, 
           RelativeSource={RelativeSource Mode=FindAncestor, 
                   AncestorType={x:Type ItemsControl}}}" /> 
+0

hmm dobra wskazówka, ale nadal pojawia się błąd. Ale teraz mówi: Nie można rozwiązać właściwości "..." w kontekście danych typu "System.Windows.Controls.ItemsControl" nooooo :( –

+0

Mój zły, ja zredagowałem kod, proszę spróbuj ponownie –

+0

wydaje się, że nadal Używam caliburns MVVM. (przepraszam, zapomniałem o tym wspomnieć) –

3

Zrobiłem szybki i pełny przykład na to:

<Window x:Class="ParentDataContext.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <DataGrid ItemsSource="{Binding items}" AutoGenerateColumns="False"> 
     <DataGrid.Columns> 
      <DataGridTemplateColumn> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Horizontal"> 
          <CheckBox IsChecked="{Binding IsChecked}"></CheckBox> 
          <TextBlock Margin="5" 
             Text="{Binding Path=DataContext.TextFromParent,RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/> 
         </StackPanel> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
     </DataGrid.Columns> 
    </DataGrid> 
</Grid> 

Kontekst dla każdego wiersza jest ustawiony do każdego obiektu z listy związanej. W naszym przypadku do każdej instancji Modelu z kolekcji przedmiotów.

Aby wrócić do rodziców DataContext składnia ta jest używana:

Text="{Binding Path=DataContext.TextFromParent,RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/> 

Oto codebehind:

public partial class MainWindow : Window 
{ 
    public string TextFromParent 
    { 
     get { return (string)GetValue(TextFromParentProperty); } 
     set { SetValue(TextFromParentProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for TextFromParent. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty TextFromParentProperty = 
     DependencyProperty.Register("TextFromParent", typeof(string), typeof(MainWindow), new PropertyMetadata(string.Empty)); 


    public ObservableCollection<Model> items { get; set; } 
    public MainWindow() 
    { 
     InitializeComponent(); 
     items = new ObservableCollection<Model>(); 
     items.Add(new Model() { IsChecked = true }); 
     items.Add(new Model() { IsChecked = false }); 
     items.Add(new Model() { IsChecked = true }); 
     items.Add(new Model() { IsChecked = false }); 
     TextFromParent = "test"; 
     this.DataContext = this; 
    } 
} 

Można zdefiniować swoją właściwość zależność w ViewModel.

I tu jest mój prosty Model:

public class Model : INotifyPropertyChanged 
{ 
    private bool _IsChecked; 

    public bool IsChecked 
    { 
     get { return _IsChecked; } 
     set 
     { 
      _IsChecked = value; 
      PropertyChanged(this, new PropertyChangedEventArgs("IsChecked")); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged = delegate { }; 
} 

W rezultacie, można uzyskać dostęp do właściwości zdefiniowane na rodzica DataContext.

enter image description here

+0

Dzięki bro. Pomogło –

Powiązane problemy