1

我得到了这样的 xaml 代码:

<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}"
4

1 回答 1

3

您可以通过使用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}"/>

改变IsActive了Ctor中的值

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; }
}
于 2013-09-26T12:27:57.733 回答