簡體   English   中英

使用引用的ItemsSource綁定到SelectedItem

[英]Binding to SelectedItem with a referenced ItemsSource

介紹

我有一個不同的DataSource池。 我有面具。 面具有索引線。 每個Indexline都有一個來自相關池的DataSource:

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; }
}

依賴屬性

在我的MainWindow上,我有一些依賴屬性(對它們沒什么特別的):

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

樣本數據

這是我的示例數據,它在WindowLoaded事件中設置:

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

這是我窗口的xaml代碼:

<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>

我現在在ListBox選擇一個掩碼,然后在DataGrid中顯示所有索引行。 到目前為止一切都很好。 當我知道從ComboBox選擇一個DataSource ,它不會存儲到Indexline對象中。 (因為當我切換掩碼,然后切換回來時,選擇消失了。當我使用調試器時,我可以看到MaskSelectedDatasource都是null)

該圖顯示了代碼的用戶界面

這種行為的原因是什么? 我需要做什么改變來獲得預期的?


有人可以提出更好的頭銜嗎? 我覺得當前的一個不是很有幫助:(

我找到了原因:

ComboBoxSelectedItem屬性的默認UpdateSourceTrigger似乎是Explicit 明確地將其設置為PropertyChanged ,解決了問題! 太簡單!

這就是新的完整XAML代碼

<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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM