如何将List传递给WPF用户控件中的listview。

huangapple go评论71阅读模式
英文:

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.

&lt;UserControl x:Class=&quot;AC_Career_Mode.controls.HistoryTab&quot;
             xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
             xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
             xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot; 
             xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot; 
             xmlns:local=&quot;clr-namespace:AC_Career_Mode.controls&quot;
             mc:Ignorable=&quot;d&quot; 
             x:Name=&quot;RecordsUC&quot;
             d:DesignHeight=&quot;450&quot; d:DesignWidth=&quot;800&quot;&gt;
    &lt;Grid&gt;
        &lt;Grid.ColumnDefinitions&gt;
            &lt;ColumnDefinition/&gt;
        &lt;/Grid.ColumnDefinitions&gt;
        &lt;Grid.RowDefinitions&gt;
            &lt;RowDefinition Height=&quot;30&quot;/&gt;
            &lt;RowDefinition/&gt;
        &lt;/Grid.RowDefinitions&gt;
        &lt;ListView x:Name=&quot;RecordsCtrl_lv&quot; SelectionMode=&quot;Single&quot; Grid.Row=&quot;1&quot; &gt;
            &lt;ListView.View&gt;
                &lt;GridView&gt;
                    &lt;GridViewColumn Width=&quot;100&quot; DisplayMemberBinding=&quot;{Binding Date, StringFormat=dd-MM-yyyy}&quot;&gt;
                        &lt;GridViewColumnHeader Content=&quot;Date&quot; /&gt;
                    &lt;/GridViewColumn&gt;

                    &lt;GridViewColumn Width=&quot;600&quot; DisplayMemberBinding=&quot;{Binding Description}&quot;&gt;
                        &lt;GridViewColumnHeader Content=&quot;Description&quot;/&gt;
                    &lt;/GridViewColumn&gt;
                &lt;/GridView&gt;
            &lt;/ListView.View&gt;
        &lt;/ListView&gt;
        &lt;Label Content=&quot;{Binding Title}&quot; HorizontalAlignment=&quot;Left&quot; Width=&quot;403&quot; /&gt;
    &lt;/Grid&gt;
&lt;/UserControl&gt;

My code behind

    public partial class HistoryTab : UserControl
    {
        public ObservableCollection&lt;Record&gt; Records 
        { 
            get { return (ObservableCollection&lt;Record&gt;)GetValue(RecordsProperty); }
            set { SetValue(RecordsProperty, value); }
        }

        public static readonly DependencyProperty RecordsProperty = DependencyProperty.Register(&quot;Records&quot;, typeof(ObservableCollection&lt;Record&gt;),
                                                                                                            typeof(HistoryTab),
                                                                                                            new PropertyMetadata(null));

        public string Title { get; set; }

        public HistoryTab()
        {
            InitializeComponent();
            DataContext = this;
        }


    }

Then I'm invoking the uc like this

&lt;uControl:HistoryTab x:Name=&quot;test_records&quot; /&gt;

and trying to set the records property like this

test_records.Records = new ObservableCollection&lt;Record&gt;(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:

&lt;!-- The HistoryTab.DataContext is implicitly inherited --&gt;
&lt;HistoryTab x:Name=&quot;test_records&quot;
            Records=&quot;{Binding RecordItems}&quot; /&gt;

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:

&lt;UserControl&gt;
  &lt;ListView x:Name=&quot;RecordsCtrl_lv&quot; 
            ItemsSource=&quot;{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=Records}&quot; /&gt;
&lt;/UserControl&gt;

huangapple
  • 本文由 发表于 2023年5月7日 05:22:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定