【问题标题】:Binding to SelectedItem with a referenced ItemsSource使用引用的 ItemsSource 绑定到 SelectedItem
【发布时间】:2012-09-20 07:14:52
【问题描述】:

简介

我有一个不同的数据源池。 我有口罩。口罩有索引线。每个 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; }
}

依赖属性

在我的主窗口上,我有一些依赖属性(它们没什么特别的):

  • AvalibleDataSources (ObservableCollection&lt;DataSource&gt;)
  • AvalibleMasks (ObservableCollection&lt;Mask&gt;)
  • SelectedMask (Mask)

样本数据

这是我的示例数据,设置在Window的Loaded事件中:

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 对象中。 (因为当我切换掩码,然后切换回来时,选择消失了。另外,当我使用调试器时,我可以看到Masks 的SelectedDatasource 都是空的)

问题

这种行为的原因是什么?我需要改变什么才能获得预期的效果?


有人能推荐一个更好的标题吗?我觉得当前的不是很有帮助:(

【问题讨论】:

  • “Xaml 很烂”怎么样。我花了 2 个小时试图获得一个使用 XAML 的简单屏幕。放弃并在 20 分钟内使用 WinForms 完成。

标签: c# wpf binding datacontext


【解决方案1】:

我找到原因了:

ComboBoxs SelectedItem 属性的默认 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> 

【讨论】:

    猜你喜欢
    • 2011-02-27
    • 1970-01-01
    • 2011-05-24
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    • 2012-04-18
    • 2017-05-11
    相关资源
    最近更新 更多