Binding to SelectedItem with a referenced ItemsSource

坚强是说给别人听的谎言 提交于 2020-01-14 19:29:06

问题


Intro

I have got a pool of different DataSources. I've got Masks. Masks have got Indexlines. Each Indexline has a single DataSource from the pool associated:

Classes

public class DataSource
{
    public string Name { get; set; }

    public override string ToString()
    {
        return Name;
    }
}

public class Mask
{
    public string Name { get; set; }
    public ObservableCollection<Indexline> Indexlines { get; set; }

    public override string ToString()
    {
        return Name;
    }
}

public class Indexline
{
    public DataSource SelectedDatasource { get; set; }
}

Dependency Properties

On my MainWindow, I've got some Dependency Properties (nothing special about them):

  • AvalibleDataSources (ObservableCollection<DataSource>)
  • AvalibleMasks (ObservableCollection<Mask>)
  • SelectedMask (Mask)

Sample data

This is my sample data, which is set in the Loaded Event of the Window:

this.AvalibleMasks = new ObservableCollection<Mask>()
{
    new Mask()
    {
        Name = "Search Mask",
        Indexlines = new ObservableCollection<Indexline>()
        {
            new Indexline(),
            new Indexline(),
            new Indexline(),
            new Indexline(),
        }
    },
    new Mask()
    {
        Name = "Document Mask",
        Indexlines = new ObservableCollection<Indexline>()
        {
            new Indexline(),
            new Indexline(),
        }
    }
};

this.AvalibleDataSources = new ObservableCollection<DataSource>()
{
    new DataSource(){Name = "ERP Database"},
    new DataSource(){Name = "CRM Database"},
};

XAML

And here's the xaml code of my window:

<Window x:Class="DataSourcesQuestion.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="MainWindow_instance"
        Title="MainWindow" Height="372" Width="735" Loaded="Window_Loaded">
    <Grid>

        <ListBox ItemsSource="{Binding AvalibleMasks}" SelectedItem="{Binding SelectedMask}" Margin="10,10,10,236" />

        <DataGrid Margin="10,111,10,43" ItemsSource="{Binding SelectedMask.Indexlines}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTemplateColumn Width="500" Header="Selected DataSource">
                    <DataGridTemplateColumn.CellTemplate>                        
                        <DataTemplate>

                            <ComboBox ItemsSource="{Binding AvalibleDataSources,Source={x:Reference MainWindow_instance}}" 
                                      SelectedItem="{Binding SelectedDatasource}"/>

                        </DataTemplate>                        
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

    </Grid>
</Window>

I now select a mask in the ListBox, then all the indexlines are displyed in the DataGrid. Everything fine so far. When I know select a DataSource from the ComboBox, it won't be stored into the Indexline object. (Cause when I swich the mask, and then switch back, the selection is gone. Also when I use the debugger, I can see the SelectedDatasource of the Masks are all null)

Question

What's the cause of this behaviour? What do I need change to get the expected one?


Can someone suggest a better title? I feel like the current one is not very helpful :(


回答1:


I found the cause:

The default UpdateSourceTrigger for the ComboBoxs SelectedItem property seems to be Explicit. Setting it to PropertyChanged explicitly, solves the problem! So easy!

So thats the new full XAML code

<Window x:Class="DataSourcesQuestion.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="MainWindow_instance" 
        Title="MainWindow" Height="372" Width="735" Loaded="Window_Loaded"> 
    <Grid> 

        <ListBox ItemsSource="{Binding AvalibleMasks}" SelectedItem="{Binding SelectedMask}" Margin="10,10,10,236" /> 

        <DataGrid Margin="10,111,10,43" ItemsSource="{Binding SelectedMask.Indexlines}" AutoGenerateColumns="False"> 
            <DataGrid.Columns> 
                <DataGridTemplateColumn Width="500" Header="Selected DataSource"> 
                    <DataGridTemplateColumn.CellTemplate>                         
                        <DataTemplate> 

                            <ComboBox ItemsSource="{Binding AvalibleDataSources,Source={x:Reference MainWindow_instance}}"  
                                      SelectedItem="{Binding SelectedDatasource, UpdateSourceTrigger=PropertyChanged}"/> 

                        </DataTemplate>                         
                    </DataGridTemplateColumn.CellTemplate> 
                </DataGridTemplateColumn> 
            </DataGrid.Columns> 
        </DataGrid> 

    </Grid> 
</Window> 


来源:https://stackoverflow.com/questions/12639389/binding-to-selecteditem-with-a-referenced-itemssource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!