C# 在UserControl内创建列表框并绑定对象
我想在UserControll中创建一个列表框,然后使用该userControl在许多页面中显示和“管理”该列表 例如,我得到了一个卡车列表,每个对象卡车都有一些属性,如名称、id 现在我创建自己的UserControlC# 在UserControl内创建列表框并绑定对象,c#,xaml,windows-runtime,windows-store-apps,C#,Xaml,Windows Runtime,Windows Store Apps,我想在UserControll中创建一个列表框,然后使用该userControl在许多页面中显示和“管理”该列表 例如,我得到了一个卡车列表,每个对象卡车都有一些属性,如名称、id 现在我创建自己的UserControl <UserControl x:Class="Crud.View.ListboxInUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://
<UserControl
x:Class="Crud.View.ListboxInUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Crud.View"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Name="myUserControl"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
<ListBox x:Name="aName" ItemsSource="{Binding ??}">
<StackPanel>
<StackPanel Orientation="Vertical" Margin="0,20,0,0">
<TextBlock Text="Id"/>
<TextBlock Text="{Binding Id}" />
</StackPanel>
<StackPanel Orientation="Vertical" Margin="0,20,0,0">
<TextBlock Text="Name"/>
<TextBlock Text="{Binding Name}" />
</StackPanel>
</StackPanel>
</ListBox>
</Grid>
如何绑定代码隐藏中的项
我如何管理列表上的“点击”
在Page.xaml中,我想写一些
<LUC:ListboxInUserControl x:Name="MyListbox DataContext="{Binding}"/>
将列表框添加到用户控件
<ListBox x:Name="aName" SelectionChanged="aName_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<StackPanel Orientation="Vertical" Margin="0,20,0,0">
<TextBlock Text="Id"/>
<TextBlock Text="{Binding Id}" />
</StackPanel>
<StackPanel Orientation="Vertical" Margin="0,20,0,0">
<TextBlock Text="Name"/>
<TextBlock Text="{Binding Name}" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
和访问选择更改事件
private void uc_ListBoxInUserControl_SelectionChangedEvent(object sender, EventArgs e)
{
}
public event EventHandler<EventArgs> SelectionChangedEvent;
public ListBoxInUserControl()
{
this.InitializeComponent();
}
private void aName_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SelectionChangedEvent(sender, new EventArgs());
}
private ListBox myVar;
public ListBox MyProperty
{
get { return aName; }
set { aName = value; }
}
<local:ListBoxInUserControl x:Name="uc_ListBoxInUserControl" SelectionChangedEvent="uc_ListBoxInUserControl_SelectionChangedEvent"> </local:ListBoxInUserControl>
uc_ListBoxInUserControl.MyProperty.ItemsSource = TestList;
private void uc_ListBoxInUserControl_SelectionChangedEvent(object sender, EventArgs e)
{
}