【发布时间】:2015-03-17 18:38:09
【问题描述】:
我创建了一个 WPF 用户控件,我想在 WPF 窗口中使用它并将其显示为列表。
我在主窗口后面的代码中有一个我添加到列表视图的数据列表,我想以 UC 也获取数据的方式将我的用户控件添加到该列表中。如果我有 3 个项目的列表,我希望我的 UC 被调用 3 次并绑定正确的数据。当我运行我的解决方案时,它可以工作,但显示 4 个空行。
我尝试使用列表,但也许另一个显示更好。
- 如何将数据设置到用户控件?
- 我做得对吗?
我的主窗口 WPF:
<Window xmlns:Controls="clr-namespace:myWpfTest.Views.Controls"
x:Class="myWpfTest.Views.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Test" Height="350" Width="525"
WindowStyle="ToolWindow" ResizeMode="NoResize"
WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Grid x:Name="MainWindows">
<Controls:UpperRibbon Height="80" VerticalAlignment="Top" />
<ListView Margin="0,90,0,0" Name="lvSessions" Background="Transparent">
<ListView.ItemTemplate>
<DataTemplate>
<Controls:Call Height="80" VerticalAlignment="Top" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Window>
我的主窗口代码在后面:
namespace myWpfTest.Views
{
public partial class Test : Window
{
private Params _params = new Params();
public Test()
{
InitializeComponent();
Background = _params.BackGroudPicture;
var param = _params.GeneralParam;
List<Session> items = new List<Session>();
items.Add(new Session() { Hour = "10 : 20", Message = "5sq465dq465d4q d 5sqd4qs65d4q ds65qd 4s65d4qs6d4q", Start = "Salle 01", End = "Bloc 01" });
items.Add(new Session() { Hour = "11 : 20", Message = "5sq465dq465d4q d 5sqd4qs65d4q ds65qd 4s65d4qs6d4q", Start = "Salle 01", End = "Bloc 01" });
items.Add(new Session() { Hour = "13 : 10", Message = "5sq465dq465d4q d 5sqd4qs65d4q ds65qd 4s65d4qs6d4q", Start = "Salle 02", End = "Bloc 01" });
items.Add(new Session() { Hour = "16 : 35", Message = "5sq465dq465d4q d 5sqd4qs65d4q ds65qd 4s65d4qs6d4q", Start = "Salle 04", End = "Bloc 02" });
lvSessions.ItemsSource = items;
}
}
public class Session
{
public string Hour { get; set; }
public string Message { get; set; }
public string Start { get; set; }
public string End { get; set; }
}
}
我的用户控件 WPF:
<UserControl x:Class="myWpfTest.Views.Controls.Call"
x:Name="CallUserControl"
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"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="500">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock x:Name="tbHour" Grid.Column="0" Grid.ColumnSpan="1" Margin="4,4,4,4" Padding="10,0,10,0" Foreground="White" FontSize="28">
<Run Text="{Binding Path=Hour}"/>
</TextBlock>
<TextBlock x:Name="Message" Grid.Column="1" Grid.ColumnSpan="1" Margin="4,4,4,4" Padding="10,0,10,0" Foreground="White" FontSize="28">
<Run Text="{Binding Path=Message}" />
</TextBlock>
<TextBlock x:Name="Start" Grid.Column="2" Grid.ColumnSpan="1" Margin="4,4,4,4" Padding="10,0,10,0" Foreground="White" FontSize="28">
<Run Text="{Binding Path=Start}"/>
</TextBlock>
<TextBlock x:Name="End" Grid.Column="3" Grid.ColumnSpan="1" Margin="4,4,4,4" Padding="10,0,10,0" Foreground="White" FontSize="28">
<Run Text="{Binding Path=End}"/>
</TextBlock>
</Grid>
</UserControl>
编辑:
主 WPF:
<Grid Name="test2"></Grid>
后面的主要代码:我将从那里开始,并根据需要调用我的 UC
Controls.Call myUC = new Controls.Call();
myUC.Hour = "test test";
Grid.SetRow(myUC, 0);
Grid.SetColumn(myUC, 0);
test2.Children.Add(myUC);
【问题讨论】:
-
我通过用一个简单的网格替换主窗口 WPF 中的 listview 来解决绑定问题。我编辑我的主要信息以显示如何。
标签: c# wpf user-controls