XAML

2015-06-18 15 views
7

'daki Itemscontrol dışındaki bir Özelliği bir Itemscontrol dışındaki bir Özelliği bağlama girişiminde bulunuyorum. Ancak bu işe yaramıyor.XAML

ItemsControl'de, DataTemplate'de koleksiyonun içinde ne olduğunu ve bunun dışında ne olduğunu belirtir. ViewModel için RelativeResource ve AncestorType'a başvurdum.

Kodu (VM):

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

XAML (Görünüm):

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

Mülklerinizi tanımlamak için nasıl benim örnek kontrol edebilir misin? Bu yardımcı olabilir. –

cevap

8

Bunu, üst ItemsControl ait DataContext bağlamak gerekir.

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

hmm iyi bir nokta, ama yine de bir hata alıyorum. Ama şimdi diyor ki: 'System.Windows.Controls.ItemsControl' türünde veri bağlamında '...' özelliği çözülemez nooooo :( –

+0

Kötüyüm, kodu yeniden düzenledim, lütfen tekrar deneyin. –

+0

Ben aynı problemi olsun. Ben MVVM caliburns kullanıyorum. (Üzgünüm, ayrıca bahsetmeyi unutmuşum) –

3

bu konuda hızlı ve tam bir örnek yaptık:

<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> 

her satır için bağlam bağlı listeden her nesneye ayarlanır. Bizim örneğimizde, her bir Model örneğine öğe koleksiyonundan.

bu sözdizimi kullanılır ebeveynin DataContext geri dönmek için:

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; 
    } 
} 

Sen ViewModel sizin bağımlılık özelliği tanımlayabilirsiniz: Burada

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

codebehind olduğunu.

Ve işte benim basit Modeli geçerli:

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 { }; 
} 

Sonuç olarak, size ebeveynin DataContext tanımlanan özelliğe erişebilir.

enter image description here

+0

Teşekkürler kardeşim. Yardımcı oldu –