英文:
How to pass a List to a listview in a WPF user control
问题
我尝试填充包含在用户控件中的列表视图,但遇到了障碍,不知道该如何继续。
<UserControl x:Class="AC_Career_Mode.controls.HistoryTab"
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:local="clr-namespace:AC_Career_Mode.controls"
mc:Ignorable="d"
x:Name="RecordsUC"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition/>
</Grid.RowDefinitions>
<ListView x:Name="RecordsCtrl_lv" SelectionMode="Single" Grid.Row="1">
<ListView.View>
<GridView>
<GridViewColumn Width="100" DisplayMemberBinding="{Binding Date, StringFormat=dd-MM-yyyy}">
<GridViewColumnHeader Content="Date" />
</GridViewColumn>
<GridViewColumn Width="600" DisplayMemberBinding="{Binding Description}">
<GridViewColumnHeader Content="Description"/>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Label Content="{Binding Title}" HorizontalAlignment="Left" Width="403" />
</Grid>
</UserControl>
我的后台代码
public partial class HistoryTab : UserControl
{
public ObservableCollection<Record> Records
{
get { return (ObservableCollection<Record>)GetValue(RecordsProperty); }
set { SetValue(RecordsProperty, value); }
}
public static readonly DependencyProperty RecordsProperty = DependencyProperty.Register("Records", typeof(ObservableCollection<Record>),
typeof(HistoryTab),
new PropertyMetadata(null));
public string Title { get; set; }
public HistoryTab()
{
InitializeComponent();
DataContext = this;
}
}
然后我像这样调用用户控件
<uControl:HistoryTab x:Name="test_records" />
并尝试设置记录属性如下
test_records.Records = new ObservableCollection<Record>(Record.DeserializeRecords(profile));
现在我不知道还需要做什么,我已经搜索过很多来源,但要么超出了我的理解,提供了很少关于要做什么的详细信息,要么太基础。
英文:
I'm trying to fill a list view contained in a user control but I have hit a roadbloack and don't know how to continue.
<UserControl x:Class="AC_Career_Mode.controls.HistoryTab"
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:local="clr-namespace:AC_Career_Mode.controls"
mc:Ignorable="d"
x:Name="RecordsUC"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition/>
</Grid.RowDefinitions>
<ListView x:Name="RecordsCtrl_lv" SelectionMode="Single" Grid.Row="1" >
<ListView.View>
<GridView>
<GridViewColumn Width="100" DisplayMemberBinding="{Binding Date, StringFormat=dd-MM-yyyy}">
<GridViewColumnHeader Content="Date" />
</GridViewColumn>
<GridViewColumn Width="600" DisplayMemberBinding="{Binding Description}">
<GridViewColumnHeader Content="Description"/>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Label Content="{Binding Title}" HorizontalAlignment="Left" Width="403" />
</Grid>
</UserControl>
My code behind
public partial class HistoryTab : UserControl
{
public ObservableCollection<Record> Records
{
get { return (ObservableCollection<Record>)GetValue(RecordsProperty); }
set { SetValue(RecordsProperty, value); }
}
public static readonly DependencyProperty RecordsProperty = DependencyProperty.Register("Records", typeof(ObservableCollection<Record>),
typeof(HistoryTab),
new PropertyMetadata(null));
public string Title { get; set; }
public HistoryTab()
{
InitializeComponent();
DataContext = this;
}
}
Then I'm invoking the uc like this
<uControl:HistoryTab x:Name="test_records" />
and trying to set the records property like this
test_records.Records = new ObservableCollection<Record>(Record.DeserializeRecords(profile));
Now I don't know what else I have to do, I have googled and consulted many sources but either it's beyond my understanding giving little detail on what to do or too basic
Some links I visited
Passing Generic lists to WPF usercontrol (I don't know what the answer wants me to do)
This youtube tutorial (Good but strays away from what I want to do I think)
答案1
得分: 1
以下是您要翻译的内容:
"Like always, when you want to pass data to a control use data binding.
I recommend to read Data binding overview (WPF .NET) to get a quick idea about data binding.
You also should never set the DataContext
of a custom control or UserControl
to itself. This eliminates the chance to set a binding on the control's properties. Don't set the DataContext
at all. It will be inherited or explicitly set in the context the control is used. This allows the following usage:
<!-- The HistoryTab.DataContext is implicitly inherited -->
<HistoryTab x:Name="test_records"
Records="{Binding RecordItems}" />
DataContext = this;
is only acceptable in a WPF root element like for example Window
, Page
and Popup
. Because by definition these elements can't have a parent (in the visual tree), there won't be a parent data related context.
First fix the control's DataContext
:
HistoryTab.xaml.cs
partial class HistoryTab : UserControl
{
public HistoryTab()
{
InitializeComponent();
}
}
Then bind the internal ListView
to the parent's Record
property:
<UserControl>
<ListView x:Name="RecordsCtrl_lv"
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=Records}" />
</UserControl>
英文:
Like always, when you want to pass data to a control use data binding.
I recommend to read Data binding overview (WPF .NET) to get a quick idea about data binding.
You also should never set the DataContext
of a custom control or UserControl
to itself. This eliminates the chance to set a binding on the control's properties. Don't set the DataContext
at all. It will be inherited or explicitly set in the context the control is used. This allows the following usage:
<!-- The HistoryTab.DataContext is implicitly inherited -->
<HistoryTab x:Name="test_records"
Records="{Binding RecordItems}" />
DataContext = this;
is only acceptable in a WPF root element like for example Window
, Page
and Popup
. Because by definition these elements can't have a parent (in the visual tree), there won't be a parent data related context.
First fix the control's DataContext
:
HistoryTab.xaml.cs
partial class HistoryTab : UserControl
{
public HistoryTab()
{
InitializeComponent();
}
}
Then bind the internal ListView
to the parent's Record
property:
<UserControl>
<ListView x:Name="RecordsCtrl_lv"
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=Records}" />
</UserControl>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论