简体   繁体   English

在XAML中正确设置绑定源

[英]Setting binding source properly in XAML

I'd like to have a list of TextBlocks with ComboBoxes next to each of them. 我想在每个旁边都有一个带有ComboBox的TextBlocks列表。 The data source of ComboBoxes should be the same for every ComboBox. 每个组合框的组合框的数据源都应该相同。 Each TextBlock however should contain sequent element of List Both data source for ComboBoxs and TextBlocks are in my "settings" object. 但是,每个TextBlock都应包含List的后续元素。ComboBoxs和TextBlocks的数据源都在我的“设置”对象中。 So I set DataContext of the whole window to this settings object. 因此,我将整个窗口的DataContext设置为此设置对象。

Here's my problem: Data source of TextBlock is: List called Fields, which is inside of an object called "Header" of type "Line" (which is of course inside settings object, which is my datacontext). 这是我的问题:TextBlock的数据源是:名为Fields的列表,该列表位于类型为“ Line”的名为“ Header”的对象内(当然,该对象位于settings对象内,这是我的datacontext)。

So, graphically: settings(type: Settings) - Header(type: CsvLine) - Fields(type: List of string) 因此,以图形方式:设置(类型:设置)-标题(类型:CsvLine)-字段(类型:字符串列表)

Now ComboBox. 现在是ComboBox。 Data source of every ComboBox should be a List called Tags 每个ComboBox的数据源都应该是一个称为标签的列表

Graphically: settings(type: Settings) - Tags(type: List of string) 图形:设置(类型:设置)-标签(类型:字符串列表)

I don't know how I should point to these locations, I tried a lot of options, but none of them work. 我不知道该如何指向这些位置,我尝试了很多选择,但没有一个起作用。 I see just a blank window. 我只看到一个空白窗口。

Here's my code: 这是我的代码:

<Grid>
    <StackPanel Orientation="Horizontal">
        <ItemsControl ItemsSource="{Binding Headers}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Fields}"/>
                        <ComboBox ItemsSource="{Binding DataContext.Tags,
                            RelativeSource={RelativeSource AncestorType=ItemsControl}}">
                        </ComboBox>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
</Grid>

I have no idea what I should actually pass as ItemsSource to ItemsControl, because I think it should be common source for both TextBoxes and ComboBoxes, but their only common source is settings object - but i already set it as my DataContext. 我不知道我应该将什么实际作为ItemsSource传递给ItemsControl,因为我认为它应该是TextBoxes和ComboBoxes的通用源,但是它们唯一的通用源是设置对象-但我已经将其设置为DataContext。 I have used RelativeSource in ComboBox, but I'm not really sure what it's used for (although I read an article about it on MSDN). 我已经在ComboBox中使用了RelativeSource,但是我不太确定它的用途(尽管我在MSDN上阅读了有关它的文章)。 I don't know why but it's really hard for me to understand binding - I'm struggling to get anything working. 我不知道为什么,但是我很难理解绑定-我正在努力使任何事情生效。

//EDIT: Here's my Settings class - which is the type of my settings object: //编辑:这是我的设置类-这是我的设置对象的类型:

public class Settings
{
    public CsvLine AllHeaders1
    {
        get
        {
            return _allHeaders1;
        }
    }

    public CsvLine _allHeaders1 = new CsvLine()
    {
        Fields = new List<string>()  
        { 
           "Header1" , "Header2" , "Header3" 
        }
    };

    private List<String> _tags;

    public List<String> Tags
    {
        get
        {
            return new List<string>() { "Tag1", "Tag2", "Tag3", "Tag4", "Tag5" };
        }
        set
        {
            _tags = value;
        }
    }

}

And here's my CsvLine class: 这是我的CsvLine类:

public class CsvLine
{
    public List<string> Fields = new List<string>();

    public int LineNumber;

}

So, I'm not 100% sure of what it is you want, but the following should get you started. 因此,我不是100%地确定您想要什么,但是以下内容应该可以帮助您入门。

Firstly, you need to ensure you bind to public properties - not public members - so the CsvLine.Fields member needs to be changed to public List<string> Fields { get { return _fields; } set { _fields = value; } } 首先,您需要确保绑定到公共属性,而不是公共成员,因此CsvLine.Fields成员需要更改为public List<string> Fields { get { return _fields; } set { _fields = value; } } public List<string> Fields { get { return _fields; } set { _fields = value; } } public List<string> Fields { get { return _fields; } set { _fields = value; } } . public List<string> Fields { get { return _fields; } set { _fields = value; } } Also not that, if you want changes in the settings object to be reflected in the UI, you will need to implement INotifyPropertyChanged. 此外,如果您希望将设置对象中的更改反映在UI中,则需要实现INotifyPropertyChanged。

Anyway, with this in place and assigned to the DataContext of the grid, the following will display a vertical list of text blocks (showing "Header 1", "Header 2", "Header 3") each with a combo box to the right containing the values "Tag1", "Tag2" ... "Tag5". 无论如何,将其放置在适当的位置并分配给网格的DataContext后,下面的内容将显示文本块的垂直列表(显示“ Header 1”,“ Header 2”,“ Header 3”),每个文本块的右侧均带有一个组合框包含值“ Tag1”,“ Tag2” ...“ Tag5”。

<Grid x:Name="SourceGrid">
    <ItemsControl ItemsSource="{Binding Path=AllHeaders1.Fields}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding}" />
                    <ComboBox ItemsSource="{Binding ElementName=SourceGrid, Path=DataContext.Tags}" />
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM