Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.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中的ItemsControl_C#_Wpf_Dictionary_Itemscontrol - Fatal编程技术网

将字典绑定到C#/WPF中的ItemsControl

将字典绑定到C#/WPF中的ItemsControl,c#,wpf,dictionary,itemscontrol,C#,Wpf,Dictionary,Itemscontrol,我正在尝试将字典中的KeyValuePair元素绑定到ItemsControl。 我的字典有15个元素,下面的代码显示了15个文本框: <WrapPanel Name="PersonsWrapPanel" Grid.Row="0"> <ItemsControl ItemsSource="{Binding Persons}" > <ItemsControl.ItemsPanel> <ItemsPanelTem

我正在尝试将字典中的KeyValuePair元素绑定到ItemsControl。 我的字典有15个元素,下面的代码显示了15个文本框:

<WrapPanel Name="PersonsWrapPanel" Grid.Row="0">
    <ItemsControl ItemsSource="{Binding Persons}" >
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Horizontal" Width="auto">
                </WrapPanel>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                    <TextBox Text="{Binding Value.Text}"></TextBox>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</WrapPanel>

不幸的是,没有任何文本框内容(可能是键或值)。
有什么想法吗?

也许可以尝试直接绑定到字典的值:

ItemsSource="{Binding Persons.Values}"
如果我正确理解了您的XAML,那么字典中的每个对象都有一个名为“Text”的字段,您正试图绑定到该字段。如果是这样,并且您进行了上述更改,则还需要更改您的数据模板:

<TextBox Text="{Binding Text}" />


有关更多信息,请参阅。HTH.

我用这条线解决了这个问题:

  <TextBox Text="{Binding Value, Mode=OneWay}"></TextBox>  


上的代码似乎不起作用。

假设您有一个名为RowValues的字典,[key,value]都定义为[string,string]

现在,要绑定到此字典的值对,可以执行以下操作:

<ItemsControl ItemsSource="{Binding RowValues.Values}" >

要显示文本(值),可以绑定为:

<TextBlock Text="{Binding}"/>