ListBox不显示绑定数据

时间:2013-03-19 08:51:56

标签: c# wpf xaml

在我的Xaml中,我有这个,

<ComboBox x:Name="masterCB" HorizontalAlignment="Left" Margin="5,127,0,0" VerticalAlignment="Top" Width="106" Height="22" Background="White" ItemsSource="{Binding Masters}" FontSize="11" SelectedIndex="{Binding Action}"/>
        <Label Content="Select Action:" HorizontalAlignment="Left" VerticalAlignment="Top" Height="26" Width="116" Margin="0,151,0,0" Background="{x:Null}" FontWeight="Bold"/>

<ListBox x:Name="actionBox" HorizontalAlignment="Left" Height="250" VerticalAlignment="Top" Width="116" Margin="5,177,0,0" ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
            ItemsSource="{Binding ActionList}" AlternationCount="2" MouseDoubleClick="actionBox_DoubleClick">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Background="{x:Null}">
                        <TextBlock Text="{Binding}" TextTrimming="WordEllipsis" TextWrapping="Wrap" Height="40" FontSize="11" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Style.Triggers>
                        <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                            <Setter Property="Background" Value="Silver"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListBox.ItemContainerStyle>

在我的数据课中,我有这个,

private List<String> actionList;
public int Action 
   { 
       get{return this.action;} 
       set
       {
           action = value;
           populateActionList();
       } 
   }
 public List<String> ActionList
   {
       get { return actionList; }
       set { this.actionList = value; }
   private void populateActionList()
   {
       if (this.action == 0)
       {
           actionList = new List<String> { 
       "Chinese",
       "Indian",
       "Malay",
       "Indian",
       };
       if (this.action == 1)
       {
           actionList = new List<String> { 
       "Dog",
       "Cats",
       "Pigs",
       "Horses",
       "Fish",
       "Lion"};
       }
   }

当我执行printline时,我的actionList被更改,但ListBox项未设置为我的actionList。只想知道为什么以及如何更新UI?我在某处读到你必须更改为DataContext和ObservableCollections,但我尝试了一次,它仍然没有更新。有谁知道为什么?

4 个答案:

答案 0 :(得分:3)

将您的代码更改为以下内容,可以达到预期效果

private ObservableCollection<String> _actionList; 

public ObservableCollection<String> ActionList {
  get { 
    if (_actionList == null) {
      _actionList = new ObservableCollection<String>();
    }

    return actionList; 
  }
}

private void populateActionList(){
  if (this.action == 0) {
    ActionList.Clear();

    ActionList.Add("Chinese");
    ActionList.Add("Indian");
    ActionList.Add("Malay");
    ActionList.Add("Indian");
  }

  if (this.action == 1){
    ActionList.Clear();

    ActionList.Add("Dog");
    ActionList.Add("Cats");
    ActionList.Add("Pigs");
    ActionList.Add("Horses");
    ActionList.Add("Fish");
    ActionList.Add("Lion");
  }
}

答案 1 :(得分:2)

我复制了所有代码,并在新的WPF应用程序中运行它。

为了解决您的绑定问题,有两种方法:

1st:在主窗口上创建一个Loaded事件,代码如下:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    DataContext = this;
    populateActionList();
}

这在我的应用中提供了以下内容:

pic of app

第二:你也可以像这样在XAML中绑定它:

您在MainWindow.xaml中的

将以下内容添加到窗口标记中:

DataContext =“{Binding RelativeSource = {RelativeSource Self}}”

然后在构造函数中,确保在运行InitializeComponent()之前设置数据:

public MainWindow()
{
   populateActionList();
   InitializeComponent();
}

答案 2 :(得分:2)

在您的情况下,您也可以根据需要使用列表,因为您要替换整个列表而不添加/删除单个项目。为此,你必须使用像Jehof写的ObservableCollection,当对集合执行add / remove / clear / replace操作时,它会自动触发CollectionChanged事件( not PropertyChanged)。

您需要更改两件事才能使代码正常工作:

  1. 在viewmodel中正确实现INotifyPropertyChanged。对于数据绑定的每个属性,您需要在setter中触发PropertyChanged事件。请参阅下面的代码。
  2. 当您想要更新绑定时,请不要更改您的私有actionList字段,而是更改您的属性ActionList,因为当您更改字段时,不会触发PropertyChanged事件。
  3. 要记住的事情是:您的UI侦听其数据绑定到的属性的PropertyChanged和CollectionChanged事件。因此,如果您的UI未更新,则通常不会触发这些事件。

    ViewModel:

    public class ViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    
        // Masters
        private List<string> _masters = new List<string>() { "Actions 0", "Actions 1" };
        public List<string> Masters { get { return _masters; } set { _masters = value; OnPropertyChanged("Masters"); } }
    
        // Action
        private int action;
        public int Action { get { return this.action; } set { action = value; PopulateActionList(); OnPropertyChanged("Action"); } }
    
        // ActionList
        private List<String> actionList;
        public List<String> ActionList { get { return actionList; } set { this.actionList = value; OnPropertyChanged("ActionList"); } }
    
    
        private void PopulateActionList()
        {
            if (this.action == 0)
                this.ActionList = new List<String> { "Chinese", "Indian", "Malay", "Indian" };
            else if (this.action == 1)
                this.ActionList = new List<String> { "Dog", "Cats", "Pigs", "Horses", "Fish", "Lion" };
        }
    }
    

答案 3 :(得分:1)

ActionList应为ObservableCollection<string>

您应该将窗口的DataContext设置为具有属性ActionList

的对象
相关问题