简体   繁体   English

将组合框绑定到列表 <string> 在WPF中

[英]Binding a combobox to a List<string> in wpf

This is a silverlight app. 这是一个Silverlight应用程序。

I am trying to bind a combobox to a list of strings. 我正在尝试将组合框绑定到字符串列表。 It will display values the I have stored in the list prior to loading the application, but I also have it so the user can add strings to the list, and these changes are not reflected in the combobox. 它将显示我在加载应用程序之前存储在列表中的值,但是我也有它,以便用户可以将字符串添加到列表中,并且这些更改不会反映在组合框中。 I have confirmed the list is being updated correctly, just not the combobox. 我已经确认列表已正确更新,而不是组合框。

The way the below code is currently, when I update the list at runtime then click the combobox dropdown, the entire silverlight app disappears. 目前,下面的代码是这样的,当我在运行时更新列表,然后单击组合框下拉菜单时,整个silverlight应用程序都会消失。 It doesn't throw any errors that I can see in VS, debug output, or in the browser, the browser window containing the silverlight app just goes blank. 它不会引发我在VS,调试输出或浏览器中看到的任何错误,包含silverlight应用程序的浏览器窗口只是空白。

 <UserControl x:Class="SilverlightSpeak.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:local="clr-namespace:SilverlightSpeak.ViewModels"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <UserControl.Resources>
        <local:MainViewModel x:Key="MainViewModel"/>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" Background="White" HorizontalAlignment="Center" VerticalAlignment="Top" DataContext="{StaticResource MainViewModel}">
        <Grid.RowDefinitions>
            <RowDefinition Height="23*"/>
            <RowDefinition Height="21*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="193*"/>
            <ColumnDefinition Width="60*"/>
        </Grid.ColumnDefinitions>
        <Button Content="Add" Command="{Binding AddWordCommand}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Grid.Column="1"/>
        <TextBox Text="{Binding ModelSpeaker.newWord, Mode=TwoWay}" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <ComboBox  ItemsSource="{Binding ModelSpeaker.vocab, Mode=TwoWay}"  SelectedIndex="{Binding ModelSpeaker.nextWordToSpeak, Mode=TwoWay}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Grid.Row="1"/>
        <Button Content="Speak" Command="{Binding SpeakWordCommand}" CommandParameter="{Binding ModelSpeaker.nextWordToSpeak}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Grid.Row="1" Grid.Column="1" Grid.RowSpan="2"/>

    </Grid>
</UserControl>

Is there something I'm doing wrong in my xaml here? 我的xaml在这里发生问题吗? I have INotifyPropertyChanged implemented in my MainViewModel and Speaker Model class and the notifications are being sent when vocab is getting updated. 我在MainViewModel和Speaker Model类中实现了INotifyPropertyChanged,并且在更新vocab时发送了通知。 I'm at a loss of how to fix this. 我不知道如何解决此问题。

Thanks in advance for any help. 在此先感谢您的帮助。

In order to get updates when a collection changes, the collection needs to implement INotifyCollectionChanged . 为了在集合更改时获取更新,该集合需要实现INotifyCollectionChanged List<T> does not implement this, but ObservableCollection<T> does, and works just like a List . List<T>不能实现此功能,但是ObservableCollection<T>可以实现,并且就像List一样工作。

So, try changing your type from List<string> to ObservableCollection<string> . 因此,尝试将您的类型从List<string>更改为ObservableCollection<string>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM