Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否将ItemsControl中的项的属性绑定到ItemsSource之外的值?_C#_Wpf_Xaml - Fatal编程技术网

C# 是否将ItemsControl中的项的属性绑定到ItemsSource之外的值?

C# 是否将ItemsControl中的项的属性绑定到ItemsSource之外的值?,c#,wpf,xaml,C#,Wpf,Xaml,我有一个ItemsControl和一个人员列表。人员列表中的每个元素都包含该人员的姓名,而不包含其他内容。在c#代码中,我将testItemsControl.ItemsSource设置为包含每个人姓名的可观察集合。公司是在代码背后定义的。下面的xaml代码正确地找到了名称,但当然找不到公司 <ItemsControl x:Name="testItemsControl"> <ItemsControl.ItemTemplate>

我有一个ItemsControl和一个人员列表。人员列表中的每个元素都包含该人员的姓名,而不包含其他内容。在c#代码中,我将testItemsControl.ItemsSource设置为包含每个人姓名的可观察集合。公司是在代码背后定义的。下面的xaml代码正确地找到了名称,但当然找不到公司

    <ItemsControl x:Name="testItemsControl">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <TextBlock Text="{Binding Name}"/>
                    <TextBlock Text="{Binding Company}"/>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>


如何正确绑定公司?

您定义的每个数据模板都使用ItemsControl.ItemsSource中的一个对象作为DataContext。在您的情况下,它是一个人类

因此,在DataTemplate中,它正在查找内容名称和公司属性。在本例中,Person.Name、Person.Company


如果要查找公司,可以将公司属性添加到person类,或设置绑定路径以查找公司属性。后者取决于相对于itemsSource定义company属性的位置。您定义的每个数据模板都使用ItemsControl.itemsSource中的一个对象作为DataContext。在您的情况下,它是一个人类

因此,在DataTemplate中,它正在查找内容名称和公司属性。在本例中,Person.Name、Person.Company


如果要查找公司,可以将公司属性添加到person类,或设置绑定路径以查找公司属性。后者取决于相对于itemsSource定义company属性的位置创建一个类来保存Name和company,用新创建类型的对象组成列表,并将其设置为itemsSource

internal class Worker 
{
    public string Name { get; set; }
    public string Company { get; set; }
}

创建一个类来保存Name和Company,用新创建类型的对象组成列表,并将其设置为itemssource

internal class Worker 
{
    public string Name { get; set; }
    public string Company { get; set; }
}

您必须使用相对资源绑定

代码隐藏

public partial class Window3 : Window
{
    public Window3()
    {
        InitializeComponent();
        this.DataContext = this;
        BuildData();
        Company = "XYZ";
        testItemsControl.ItemsSource = Persons;
    }

    private void BuildData()
    {
        Persons.Add(new Person() { Name = "R1" });
        Persons.Add(new Person() { Name = "R2" });
        Persons.Add(new Person() { Name = "R3" });
    }

    public string Company { get; set; }

    private ObservableCollection<Person> _persons = new ObservableCollection<Person>();

    public ObservableCollection<Person> Persons
    {
        get { return _persons; }
        set { _persons = value; }
    }
}
公共部分类窗口3:窗口
{
公共窗口3()
{
初始化组件();
this.DataContext=this;
BuildData();
Company=“XYZ”;
testItemsControl.ItemsSource=人;
}
私有void BuildData()
{
添加(newperson(){Name=“R1”});
添加(newperson(){Name=“R2”});
添加(newperson(){Name=“R3”});
}
公共字符串公司{get;set;}
私人可观测集合_persons=新可观测集合();
公众人士
{
获取{return\u persons;}
设置{u persons=value;}
}
}
XAML代码

<ItemsControl x:Name="testItemsControl">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}" Margin="5"/>
                    <TextBlock Text="{Binding Company, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" Margin="5" />

                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

谢谢,
Rajnikant

您必须使用相对资源绑定

代码隐藏

public partial class Window3 : Window
{
    public Window3()
    {
        InitializeComponent();
        this.DataContext = this;
        BuildData();
        Company = "XYZ";
        testItemsControl.ItemsSource = Persons;
    }

    private void BuildData()
    {
        Persons.Add(new Person() { Name = "R1" });
        Persons.Add(new Person() { Name = "R2" });
        Persons.Add(new Person() { Name = "R3" });
    }

    public string Company { get; set; }

    private ObservableCollection<Person> _persons = new ObservableCollection<Person>();

    public ObservableCollection<Person> Persons
    {
        get { return _persons; }
        set { _persons = value; }
    }
}
公共部分类窗口3:窗口
{
公共窗口3()
{
初始化组件();
this.DataContext=this;
BuildData();
Company=“XYZ”;
testItemsControl.ItemsSource=人;
}
私有void BuildData()
{
添加(newperson(){Name=“R1”});
添加(newperson(){Name=“R2”});
添加(newperson(){Name=“R3”});
}
公共字符串公司{get;set;}
私人可观测集合_persons=新可观测集合();
公众人士
{
获取{return\u persons;}
设置{u persons=value;}
}
}
XAML代码

<ItemsControl x:Name="testItemsControl">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}" Margin="5"/>
                    <TextBlock Text="{Binding Company, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" Margin="5" />

                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

谢谢,
Rajnikant

谢谢你的回答。出于我的目的,我不想将公司属性添加到person的类中。您能告诉我有关设置“查找公司属性的绑定路径”的更多信息吗?谢谢您的回答。出于我的目的,我不想将公司属性添加到person的类中。您能告诉我有关设置“查找公司属性的绑定路径”的更多信息吗?非常感谢您的回答。它完美地回答了前面提到的问题,但我应该提到我打算将其用于WindowsPhone8开发,而WindowsPhone8似乎不支持AncestorType属性。您知道其他解决方案吗?非常感谢您的回答。它完美地回答了前面提到的问题,但我应该提到我打算将其用于WindowsPhone8开发,而WindowsPhone8似乎不支持AncestorType属性。您是否知道另一种解决方案?