繁体   English   中英

将Listview内的组合框绑定到ObservableCollection <string>

[英]Binding Combobox inside a Listview to a ObservableCollection<string>

XAML

<UserControl x:Class="PatientsInscrit_GMF.ListDisplay"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:custom="clr-namespace:PatientsInscrit_GMF"
             DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="25"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <ListView Grid.Row="1" Margin="0,0,0,0" x:Name ="FileList">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Tag" Width="Auto">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox ></ComboBox>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>   
    </Grid>
</UserControl>

.CS

namespace PatientsInscrit_GMF
{
    public partial class ListDisplay : UserControl
    {
        private ObservableCollection<string> _tags;

        public ObservableCollection<string> Tags
        {
            get { return _tags; }
        }

        public ListDisplay()
        {
            InitializeComponent();
            _tags = new ObservableCollection<string>();
            List<string> tags = Facade.Instance.RetrieveTags();
            foreach(string s in tags)
            {
                _tags.Add(s);
            }
        }
    }
}

我如何将标签绑定到“组合框”(我的列表视图中的许多列之一)?

我试过了

ItemsSource = "{Binding Tags}"

但是组合框仍然为空。 我肯定会确定Facade List检索不为空,因为创建它时我添加了2个默认标签,因此它也不为空。

我尝试了其他各种解决方案,但都没有用,甚至有甚至无法编译的代码示例。

请帮忙 :)

我相信在(最初)获取Tags属性时,尚未初始化_tags _tags初始化之后,可以引发Tags属性更改(通过实现INotifyPropertyChanged )。 但是,该属性似乎只需要初始化(更改)一次。 因此,实现这种接口并不是真正必要的。 您可以尝试以下代码,以确保在获取Tags ,将初始化数据:

public ObservableCollection<string> Tags {
   get { 
      if(_tags == null){
        //move your code after InitializeComponent() to here
        _tags = new ObservableCollection<string>();
        List<string> tags = Facade.Instance.RetrieveTags();
        foreach(string s in tags) {
            _tags.Add(s);
        }
      }
      return _tags;
   }
}

在XAML中:

<ComboBox ItemsSource="{Binding Tags,
          RelativeSource={RelativeSource AncestorType=UserControl}}"></ComboBox>

为了使用DataBind,请遵循以下步骤:

1)确保已将DataContext(在UserControl标记中)设置为DataContext="{Binding RelativeSource={RelativeSource Self}}"

2)您需要将ComboBox中的ItemsSource绑定更改为: ItemsSource="{Binding SomePropertyName}"

3)您需要在支持CS文件中实现`INotifyPropertyChanged'

4)要实现此属性,请使用以下命令:

    #region INotifyPorpertyChanged Memebers 

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(string propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion

5)使类型为ObservableCollection的属性成为所需对象的类型。 此属性是ComboBox绑定到的属性。

例如:

private ObservableCollection<SomeDataType> _myPrivateData;
public ObservableCollection<SomeDataType> SomePropertyName { get { return _myPrivateData; } set    { _myPrivateData= value; NotifyPropertyChanged("SomePropertyName"); } }

这将处理数据绑定部分。 现在,每次重置集合时,DataGrid绑定到DataGrid的集合都会更新,因为调用了NotifyPropertyChanged。

暂无
暂无

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

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