Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.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# KeyValuePair上ItemTemplate中的绑定不起作用_C#_Binding_Microsoft Metro_.net 4.5 - Fatal编程技术网

C# KeyValuePair上ItemTemplate中的绑定不起作用

C# KeyValuePair上ItemTemplate中的绑定不起作用,c#,binding,microsoft-metro,.net-4.5,C#,Binding,Microsoft Metro,.net 4.5,在ListView的ItemTemplate中进行绑定时遇到问题。我的绑定目标是KeyValuePair。以下代码: XAML: C#: 受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e){ 如果(例如参数!=null){ IDataSourceExtension=(IDataSourceExtension)e.Parameter; pageTitle.Text=扩展名.Name; //LastData的类型:列表 listViewDataItems.I

在ListView的ItemTemplate中进行绑定时遇到问题。我的绑定目标是KeyValuePair。以下代码:

XAML:


C#:

受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e){
如果(例如参数!=null){
IDataSourceExtension=(IDataSourceExtension)e.Parameter;
pageTitle.Text=扩展名.Name;
//LastData的类型:列表
listViewDataItems.ItemsSource=extension.LastData;
}
}
ItemsSource设置良好,包含KeyValuePairs,但在ListView中显示键和值无效。我在metro风格的应用程序中使用.Net4.5测试版。

我测试了以下内容:

public ObservableCollection<KeyValuePair<string, object>> LvItems { get; set; }

public MainWindow()
{
   InitializeComponent();
   this.DataContext = this;
   LvItems = new ObservableCollection<KeyValuePair<string, object>>();
   LvItems.Add(new KeyValuePair<string, object>("Idx", 5));
   LvItems.Add(new KeyValuePair<string, object>("Ido", 12));
}
public observeCollection LvItems{get;set;}
公共主窗口()
{
初始化组件();
this.DataContext=this;
LvItems=新的ObservableCollection();
添加(新的KeyValuePair(“Idx”,5));
添加(新的KeyValuePair(“Ido”,12));
}
并使用了你的项目模板,它就像一个符咒

所以我最好的猜测是:

1) 您需要使用ObservableCollection来通知集合中的更改(否则项目可能在此处,但ListView不会刷新,因为它不知道集合已更改)

2) 您应该避免直接设置集合:将其设置为只读,并逐个添加项,而不是设置集合。(或在集合的setter中提升属性更改)

编辑:完整的代码

<Window x:Class="ConverterCombinerTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ConverterCombinerTest"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
         <ListView ItemsSource="{Binding LvItems}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=Key}"/>
                        <TextBlock Text=":"/>
                        <TextBlock Text="{Binding Path=Value}"/>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>

public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
            LvItems = new ObservableCollection<KeyValuePair<string, object>>();
            LvItems.Add(new KeyValuePair<string, object>("Idx", 5));
            LvItems.Add(new KeyValuePair<string, object>("Ido", 12));
        }

        public ObservableCollection<KeyValuePair<string, object>> LvItems { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(String _Prop)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(_Prop));
            }
        }
    }

公共部分类主窗口:窗口,INotifyPropertyChanged
{
公共主窗口()
{
初始化组件();
this.DataContext=this;
LvItems=新的ObservableCollection();
添加(新的KeyValuePair(“Idx”,5));
添加(新的KeyValuePair(“Ido”,12));
}
公共ObservableCollection LvItems{get;set;}
公共事件属性更改事件处理程序属性更改;
私有void raiseProperty已更改(字符串_Prop)
{
PropertyChangedEventHandler处理程序=this.PropertyChanged;
if(处理程序!=null)
{
处理程序(此,新属性changedeventargs(_Prop));
}
}
}

我两个都试过了,但都没用。列表的Setter已调用PropertyChanged(),ObservableCollection没有帮助。一个接一个地设置项目也没用。我修改了代码并编辑了您的集合LvItems,而不是LastData,它也不会显示。是否正确设置了DataContext?我发布的示例与您的确切XAML一起在我的计算机上工作。仍然显示正确数量的项目,但仅显示“:”而不是“键:值”。您是否使用.net4.5测试版对其进行了测试?设置整个Windows/UserControl的DataContext,而不是每个特定元素的DataContext。我添加了我的全部代码。不起作用:/我创建了一个新的干净的metro风格的项目,添加了你的代码,添加了我的模板,不起作用。你在metro风格的应用程序中尝试过吗?
public ObservableCollection<KeyValuePair<string, object>> LvItems { get; set; }

public MainWindow()
{
   InitializeComponent();
   this.DataContext = this;
   LvItems = new ObservableCollection<KeyValuePair<string, object>>();
   LvItems.Add(new KeyValuePair<string, object>("Idx", 5));
   LvItems.Add(new KeyValuePair<string, object>("Ido", 12));
}
<Window x:Class="ConverterCombinerTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ConverterCombinerTest"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
         <ListView ItemsSource="{Binding LvItems}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=Key}"/>
                        <TextBlock Text=":"/>
                        <TextBlock Text="{Binding Path=Value}"/>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>

public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
            LvItems = new ObservableCollection<KeyValuePair<string, object>>();
            LvItems.Add(new KeyValuePair<string, object>("Idx", 5));
            LvItems.Add(new KeyValuePair<string, object>("Ido", 12));
        }

        public ObservableCollection<KeyValuePair<string, object>> LvItems { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(String _Prop)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(_Prop));
            }
        }
    }