Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.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# WPF ListView绑定到IDictionary<;字符串,IEnumerable<;MyObjectType>&燃气轮机;_C#_Wpf_Data Binding - Fatal编程技术网

C# WPF ListView绑定到IDictionary<;字符串,IEnumerable<;MyObjectType>&燃气轮机;

C# WPF ListView绑定到IDictionary<;字符串,IEnumerable<;MyObjectType>&燃气轮机;,c#,wpf,data-binding,C#,Wpf,Data Binding,我对数据绑定没有什么问题。 我想创建如下结构: 因此,我有一个带有自定义ItemTemplate的ListView: <ListView ItemsSource="{Binding Path=Data}"> <ListView.Template> <ControlTemplate> <Border x:Name="Bd" SnapsToDevicePixels="true" Back

我对数据绑定没有什么问题。 我想创建如下结构:

因此,我有一个带有自定义ItemTemplate的ListView:

<ListView ItemsSource="{Binding Path=Data}">
        <ListView.Template>
            <ControlTemplate>
                <Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="1">
                <Grid>
                    <ScrollViewer VerticalScrollBarVisibility="Hidden"
                                  HorizontalScrollBarVisibility="Hidden"
                                  x:Name="PART_AnimatedScrollViewer"
                                  Padding="{TemplateBinding Padding}" 
                                  Focusable="false">
                                  <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </ScrollViewer>
                    </Grid>
                </Border>
            </ControlTemplate>
        </ListView.Template>
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <TextBlock Text="{Binding Key}" Background="LightGray"/>
                    <ItemsControl Grid.Row="1" ItemsSource="{Binding Value, IsAsync=True}">
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="auto"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="{Binding ItemName}"/>
                                <TextBlock Text="{Binding Description}" />
                            </Grid>
                        </DataTemplate>
                    </ItemsControl>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

以下是ViewModel:

public class ListViewModel : INotifyPropertyChanged
{
    public ListViewModel()
    {
        _data.Add("ExampleHeader1", new List<MyObjectType>()
                                              {
                                                  new MyObjectType("Example ItemName1", "Description1"),
                                                  new MyObjectType("Example ItemName2", "Description2"),
                                                  new MyObjectType("Example ItemName3", "Description3"),
                                              });
        _data.Add("ExampleHeader2", new List<MyObjectType>()
                                              {
                                                  new MyObjectType("Example ItemName1", "Description1"),
                                                  new MyObjectType("Example ItemName2", "Description2"),
                                                  new MyObjectType("Example ItemName3", "Description3"),
                                                  new MyObjectType("Example ItemName4", "Description4"),
                                              });
        _data.Add("ExampleHeader3", new List<MyObjectType>()
                                              {
                                                  new MyObjectType("Example ItemName1", "Description1"),
                                                  new MyObjectType("Example ItemName2", "Description2"),
                                                  new MyObjectType("Example ItemName3", "Description3"),
                                              });
    }

    private readonly Dictionary<string, List<MyObjectType>> _data = new Dictionary<string, List<MyObjectType>>();
    public IDictionary<string, List<MyObjectType>> Data
    {
        get { return this._data; }
    }

    private KeyValuePair<string, List<MyObjectType>>? selectedKey = null;
    public KeyValuePair<string, List<MyObjectType>>? SelectedKey
    {
        get { return this.selectedKey; }
        set
        {
            this.selectedKey = value;
            this.OnPropertyChanged("SelectedKey");
            this.OnPropertyChanged("SelectedValue");
        }
    }

    public List<MyObjectType> SelectedValue
    {
        get
        {
            if (null == this.SelectedKey)
            {
                return new List<MyObjectType>();
            }

            return this._data[this.SelectedKey.Value.Key];
        }
        set
        {
            this._data[this.SelectedKey.Value.Key] = value;
            this.OnPropertyChanged("SelectedValue");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propName)
    {
        var eh = this.PropertyChanged;
        if (null != eh)
        {
            eh(this, new PropertyChangedEventArgs(propName));
        }
    }
}
公共类ListViewModel:INotifyPropertyChanged
{
公共ListViewModel()
{
_添加(“ExampleHeader1”,新列表()
{
新的MyObject类型(“示例ItemName1”、“Description1”),
新的MyObject类型(“示例ItemName2”、“Description2”),
新的MyObject类型(“示例ItemName3”、“Description3”),
});
_添加(“ExampleHeader2”,新列表()
{
新的MyObject类型(“示例ItemName1”、“Description1”),
新的MyObject类型(“示例ItemName2”、“Description2”),
新的MyObject类型(“示例ItemName3”、“Description3”),
新的MyObject类型(“示例ItemName4”、“Description4”),
});
_添加(“ExampleHeader3”,新列表()
{
新的MyObject类型(“示例ItemName1”、“Description1”),
新的MyObject类型(“示例ItemName2”、“Description2”),
新的MyObject类型(“示例ItemName3”、“Description3”),
});
}
专用只读词典_data=new Dictionary();
公共词典数据
{
获取{返回此。_data;}
}
private KeyValuePair?selectedKey=null;
公钥值对?选择的密钥
{
获取{返回this.selectedKey;}
设置
{
this.selectedKey=值;
此.OnPropertyChanged(“SelectedKey”);
此.OnPropertyChanged(“SelectedValue”);
}
}
公共列表SelectedValue
{
得到
{
if(null==此.SelectedKey)
{
返回新列表();
}
返回此。_数据[this.SelectedKey.Value.Key];
}
设置
{
此._数据[this.SelectedKey.Value.Key]=值;
此.OnPropertyChanged(“SelectedValue”);
}
}
公共事件属性更改事件处理程序属性更改;
私有void OnPropertyChanged(字符串propName)
{
var eh=this.PropertyChanged;
if(null!=eh)
{
eh(这是新的PropertyChangedEventArgs(propName));
}
}
}
如果我运行此应用程序,我将得到以下错误:

在使用ItemsSource之前,Items集合必须为空


如何修复此错误?

使用ObservableCollection加载列表项


即使您可以更改数据绑定控件,但不建议这样做。而是使用ObservableCollection并使用您想要加载的适当列表更改其内容。由于ObservableCollection可以很容易地向其绑定的对象抛出通知,因此您的UI列表将获得UpdateAD

使用ObservableCollection加载列表项

即使您可以更改数据绑定控件,但不建议这样做。而是使用ObservableCollection并使用您想要加载的适当列表更改其内容。由于ObservableCollection可以很容易地向其绑定的对象抛出通知,因此您的UI列表将获得UpdateAD

错误在这里:

<ItemsControl Grid.Row="1" ItemsSource="{Binding Value, IsAsync=True}">
    <DataTemplate>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="{Binding ItemName}"/>
            <TextBlock Text="{Binding Description}" />
        </Grid>
    </DataTemplate>
</ItemsControl>
错误在这里:

<ItemsControl Grid.Row="1" ItemsSource="{Binding Value, IsAsync=True}">
    <DataTemplate>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="{Binding ItemName}"/>
            <TextBlock Text="{Binding Description}" />
        </Grid>
    </DataTemplate>
</ItemsControl>

谢谢=)我以为我丢失了小而重要的细节。@Leonid-这种情况经常发生,尤其是在这么晚的时候:)谢谢=)我以为我丢失了小而重要的细节。@Leonid-这种情况经常发生,尤其是在这么晚的时候:)
<ItemsControl Grid.Row="1" ItemsSource="{Binding Value, IsAsync=True}">
   <ItemsControl.ItemTemplate>
      <DataTemplate>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="{Binding ItemName}"/>
            <TextBlock Text="{Binding Description}" />
        </Grid>
      </DataTemplate>
   </ItemsControl.ItemTemplate>
</ItemsControl>