开发者

Binding a ListView to a property in the same Class

I have a Listview and i want to bind it to a list declared on the same class(codebehind)

public ObservableCollection<Slot> ListViewList { get; set; }

<ListView x:Name="ListViewSlots" Margin="0,230,0,0" ItemsPanel="{DynamicResource ItemsPanelTem开发者_C百科plate1}" ItemsSource="{Binding Path=UserControl.ListViewList}" >

But is not working, i tried setting the datacontext of the usercontrol to self and desnt works.


Have you tried setting the DataContext of the UserControl to the list, and then setting the ItemsSource of the ListView to that?

ie.

<ListView ItemsSource="{Binding}" >


Add to your Window


<Window ...
   DataContext="{Binding RelativeSource={RelativeSource Self}}"
>

and then your ListView becomes


<Listview ItemsSource="{Binding ListViewList}">...


first you have to introduce your list to the resources of the class:

public List<string> ListViewList
{
    get{ return (List<string> Resources["ListViewList"];}
    set{ Resources["ListViewList"] = value;}
}

or use ObservableCollection:

private ObservableCollection<string> _listViewList = new ObservableCollection<string>();
public ObservableCollection<string> ListViewList { get { return _listViewList; } }

then in XAML, you can bind something to it:

<ListView>
    <ItemsPanel
        ItemsPanel="{DynamicResource ItemsPanelTemplate1}"
        ItemsSource="{Binding ListViewList}"
    />
</ListView>

and as Joel said you need to set the DataContext of the entire window (or just the block you're dealing with) to self:

<Window ...
   DataContext="{Binding RelativeSource={RelativeSource Self}}"
>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜