【发布时间】:2015-01-12 12:31:25
【问题描述】:
假设我在 XAML 中有 WPF 视图:
<Window x:Class="TestingMatrix.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestingMatrix"
Title="All Learned Numbers" Height="500" Width="800">
<Window.Resources>
<!--Defining a dataTemplate-->
<DataTemplate x:Key="myTaskTemplate" DataType="{x:Type local:LearnedElement}">
<StackPanel>
<Label Content="Input:" Margin="8,6,-320,-164" />
<TextBlock Text="{Binding Path=InputTxt}" HorizontalAlignment="Left" Margin="56,9,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="48" Width="300"/>
<Label Content="Output:" HorizontalAlignment="Left" Margin="378,9,0,0" VerticalAlignment="Bottom"/>
<TextBlock Text="{Binding Path=OutputTxt}" HorizontalAlignment="Left" Margin="444,9,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="48" Width="295"/>
<DataGrid Name="myDataGrid" CanUserAddRows="False" CanUserResizeRows="False" Margin="44,66,0,0" CanUserDeleteRows="False" Focusable="False" AreRowDetailsFrozen="True" IsEnabled="False" CanUserReorderColumns="False" HorizontalAlignment="Left" VerticalAlignment="Top" HeadersVisibility="None"/>
<DataGrid Name="myDataGrid_Copy" CanUserAddRows="False" CanUserResizeRows="False" Margin="445,66,0,0" CanUserDeleteRows="False" Focusable="False" AreRowDetailsFrozen="True" IsEnabled="False" CanUserReorderColumns="False" HorizontalAlignment="Left" VerticalAlignment="Top" HeadersVisibility="None"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid Height="500" Width="800" Name="myGrid">
<!--Place a dataTemplate instance in here, it will get the values from a list of elements -->
<ListBox ItemsSource="{Binding ObservableElements}">
</ListBox>
</Grid>
</Window>
我有一个类来表示 Observable 集合列表中的每个项目。
public class LearnedElement
{
public string InputTxt {get; set;}
public string OutputTxt {get; set;}
}
我有代码隐藏:
//List of elements that will be binded with the template
public ObservableCollection<LearnedElement> observableElements { get; set; }
public Window2()
{
InitializeComponent();
ObservableElements = new ObservableCollection<LearnedElement>();
LearnedElement learnedElem = new LearnedElement();
learnedElem.InputTxt = "Example 1";
learnedElem.OutputTxt = "Example 2";
//How to create an instance of the template and add to the StackPanel ?
LearnedElement learnedElem2 = new LearnedElement();
learnedElem2.InputTxt = "Example 3";
learnedElem2.OutputTxt = "Example 4";
ObservableElements.Add(learnedElem);
this.DataContext = ObservableElements;
}
我想最终得到类似于下面绘制的东西,从元素列表中我将实例化一个新的 DataTemplate 并将其放置在 StackPanel 上:
最好的方法是什么?
已编辑,但仍无法正常工作:
将 StackPanel 更改为 ListBox
【问题讨论】:
-
使用 ItemsControl - msdn.microsoft.com/en-us/library/…
-
是的
ItemControl是要走的路,但要小心,因为ItemControl默认不支持选择。但是,您可以创建自己的ItemControl来实现ISelectable -
您应该在您的
DataTemplate上设置您的TargetType="{x:Type LearnedElement}"或将模板放入StackPanel模板中,就像这样<StackPanel.Template><DataTemplate><Label Content="Input:" Margin="8,6,-320,-164"/> ... </StackPanel.Template> -
@Franck “创建您自己的实现 ISelectable 的 ItemControl”。什么?需要选择时使用 ListBox(它是一个 ItemsControl)。
-
@Clemens 是的,但这取决于他是否需要将孩子包裹在容器中。
ListBox将换行,但ItemControl不会。No Select -> ItemControl,Select + Wrap -> ListBox,Select + No Wrap -> ItemControl + ISelectable
标签: c# wpf data-binding datatemplate