2013-09-26 53 views
0

我的XAML代碼:訪問的DataContext

<UserControl x:Class="WindowsFormsApplication4.gg" 
      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" 
      xmlns:local="clr-namespace:WindowsFormsApplication4" 
      x:Name="myUserControl" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <StackPanel> 
     <TextBox Text="{Binding ElementName=myUserControl, Path=DataContext.IsActive}"></TextBox> 
     <Grid> 
      <DataGrid x:Name="dataGrid1" AutoGenerateColumns="false" ItemsSource="{Binding MyClass}" CanUserAddRows="False" Margin="0,0,0,-119"> 
       <DataGrid.Columns> 

        <!-- Caption --> 
        <DataGridTemplateColumn Header="X"> 
         <DataGridTemplateColumn.CellTemplate> 
          <DataTemplate> 
           <TextBox VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Text="{Binding Path=stringPro}" /> 
          </DataTemplate> 
         </DataGridTemplateColumn.CellTemplate> 
        </DataGridTemplateColumn> 

        <!-- Caption --> 
        <DataGridTemplateColumn Header="Y"> 
         <DataGridTemplateColumn.CellTemplate> 
          <DataTemplate> 
           <TextBox VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Text="{Binding ElementName=myUserControl, Path=DataContext.IsActive}" /> 
          </DataTemplate> 
         </DataGridTemplateColumn.CellTemplate> 
        </DataGridTemplateColumn> 
       </DataGrid.Columns> 
      </DataGrid> 
     </Grid> 
    </StackPanel> 
</UserControl> 

代碼僅次於設置數據上下文:

this.DataContext = new SomeViewModel(); 

SomeClass.cs:

namespace WindowsFormsApplication4 
{ 
    public class SomeClass 
    { 
     public SomeClass() 
     { 
      this.MyProperty = 2; 
      this.stringPro = "abcsdfg"; 
     } 

     public int MyProperty { get; set; } 

     public string stringPro { get; set; } 
    } 
} 

SomeViewModel.cs :

namespace WindowsFormsApplication4 
{ 
    using System.Collections.Generic; 

    public class SomeViewModel 
    { 
     public SomeViewModel() 
     { 
      this.IsActive = false; 
      this.MyClass = new List<SomeClass>(); 
      this.MyClass.Add(new SomeClass()); 
      this.MyClass.Add(new SomeClass()); 
     } 

     public bool IsActive { get; set; } 

     public List<SomeClass> MyClass { get; set; } 
    } 
} 

我試圖用ElementName工作外的** ** DataGrid而不是內部

Text="{Binding ElementName=myUserControl, Path=DataContext.IsActive}" 

回答

2

你可以通過使用RelativeSource

<TextBox VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.IsActive}"/> 

測試的例子:

   <DataGrid x:Name="dataGrid1" AutoGenerateColumns="false" ItemsSource="{Binding MyClass}" CanUserAddRows="False" Margin="0,0,0,-119"> 
       <DataGrid.Columns> 

        <!-- Caption --> 
        <DataGridTemplateColumn Header="X"> 
         <DataGridTemplateColumn.CellTemplate> 
          <DataTemplate> 
           <TextBox VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Text="{Binding Path=stringPro}" /> 
          </DataTemplate> 
         </DataGridTemplateColumn.CellTemplate> 
        </DataGridTemplateColumn> 

        <!-- Caption --> 
        <DataGridTemplateColumn Header="Y"> 
         <DataGridTemplateColumn.CellTemplate> 
          <DataTemplate> 
           <TextBox VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.IsActive}"/> 
          </DataTemplate> 
         </DataGridTemplateColumn.CellTemplate> 
        </DataGridTemplateColumn> 
       </DataGrid.Columns> 
      </DataGrid> 

      <CheckBox IsChecked="{Binding IsActive}"/> 

更改在構造函數

public class SomeViewModel 
{ 
    public SomeViewModel() 
    { 
     this.IsActive = false; 
     this.MyClass = new List<SomeClass>(); 
     this.MyClass.Add(new SomeClass()); 
     this.MyClass.Add(new SomeClass()); 

     // Test it 
     IsActive = true; 
    } 

    public bool IsActive { get; set; } 

    public List<SomeClass> MyClass { get; set; } 
} 
+0

@WhileTrueSleep上傳完整的XAML和代碼0值'MyClass',我會相應地更新我的答案 –

+0

@WhileTrueSleep這是奇怪的。我能夠讓他們都工作。也許你的問題在別處 –

+1

謝謝你!採取祖先是解決它的方法.. – WhileTrueSleep