ListBox的简单WPF DataBinding到字符串的ObservableCollection

时间:2012-11-30 15:15:52

标签: c# .net wpf data-binding .net-4.0

好的,我只是不明白。请告诉我为什么我的ListBox中没有任何项目(现在应该是两个字符串“空”和“东西”):

XAML:

<Window.DataContext>
    <Windows:SettingsWindowModel x:Name="model"/>
</Window.DataContext>

<Window.Resources>
    <DataTemplate x:Key="ListItemTemplate">
        <ListBoxItem Content="{Binding}"  />
    </DataTemplate>
</Window.Resources>

<ListBox Name="listBoxActivities" SelectionChanged="ListBoxActivitiesSelectionChanged"
 ItemsSource="{Binding Path=IgnoredActivities}"
 HorizontalAlignment="Left" VerticalAlignment="Top" MinHeight="40" MinWidth="200"
 Padding="5,100,5,0" Height="100" Margin="0,207,0,0" ItemTemplate="{StaticResource ListItemTemplate}" />

在SettingsWindowModel中:

    private ObservableCollection<String> _ignoredActivities;
    public ObservableCollection<String> IgnoredActivities
    {
        get
        {
            if (_ignoredActivities == null)
            {
                // empty
                _ignoredActivities = new ObservableCollection<String>() { "empty","stuff" };
            }
            return _ignoredActivities;
        }
    }

还有什么需要知道的吗?我忘记了什么?

编辑: 也许我应该补充一点,VisualStudio + ReSharper也没有显示下划线和编译错误。甚至没有警告。

7 个答案:

答案 0 :(得分:1)

对不起伙计们,数据一直在那里。问题出在视觉细节上。填充也应用于ListBox(或项目)的子容器,因此项目不位于列表的顶部。由于我只是ListBox上的高度,所以项目总是低于ListBox的可见高度。 Stange要调试的东西。不管怎样,谢谢你的回答!

答案 1 :(得分:0)

1)您无需设置DataTemplate

2)你确定视图的DataContext(aaaaa.xaml.cs)是ViewModel(bbbbb.cs,包含IgnoredActivities)吗?

应该是这样的:

aaaaa.xaml:

<ListBox ItemsSource="{Binding IgnoredActivities}" />

aaaaa.xaml.cs:

public partial class aaaaa : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new bbbbb();
    }
}

bbbbb.cs:

    public class bbbbb : INotifyPropertyChanged
    {
        public bbbbb()
        {
            IgnoredActivities.Add("empty");
            IgnoredActivities.Add("stuff");
        }

        private ObservableCollection<String> _ignoredActivities;
        public ObservableCollection<String> IgnoredActivities
        {
            get
            {
                if (_ignoredActivities == null)
                {
                    // empty
                    _ignoredActivities = new ObservableCollection<String>() { "empty","stuff" };
                }
                return _ignoredActivities;
            }
        }

            #region INotifyPropertyChanged Members

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

            #endregion
    }
}

答案 2 :(得分:0)

使用此语句,您将绑定到窗口中名为“model”的控件中的“IgnoredActivities”属性。

ItemsSource="{Binding ElementName=model, Path=IgnoredActivities}"

使用ViewModels时,将它们设置为控件的DataContext:

<ListBox ItemsSource="{Binding IgnoredActivities}">
    <ListBox.DataContext>
        <local:MyViewModel/>
    </ListBox.DataContext>        
</ListBox>

没有指定源的绑定(“{Binding PathToMyPropert}”)默认为控件的DataContext,它继承自父控件。因此,在您的情况下,您可以将ViewModel设置为Window的DataContext,以便它的所有子项都可以使用:

<Window>
    <Window.DataContext>
        <local:MyViewModel/>
    </Window.DataContext>        

    <ListBox ItemsSource="{Binding IgnoredActivities}"/>    
</Window>

答案 3 :(得分:0)

摆脱那个ElementName绑定属性。它用于绑定到WPF控件。只需设置ItemsSource={Binding IgnoredActivities}

即可

答案 4 :(得分:0)

尝试如下:

<ItemsControl ItemsSource="{Binding Path=IgnoredActivities}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <ListBox />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
       <DataTemplate>
           <ListBoxItem Content="{Binding}"  />
       </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

答案 5 :(得分:0)

尝试在后面的代码中为控件设置DataContext。

Xaml       - <ListBox Name="myListBox" ItemsSource="{Binding IgnoredActivities}"/>    
CodeBehind - myListBox.DataContext=this;

答案 6 :(得分:-1)

确保 DataContext 包含 IgnoredActivities 并将 ItemsSource 绑定到它:

ItemsSource="{Binding IgnoredActivities}"