以编程方式-WPF添加到StackPanel DataTemplate实例

时间:2015-01-12 12:31:26

标签: c# wpf data-binding datatemplate

假设我在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上: enter image description here

最好的方法是什么?

已编辑,但仍无效:

将StackPanel更改为ListBox

1 个答案:

答案 0 :(得分:1)

使用ListBox代替StackPanel,只需将您的收藏品数据绑定到其ItemsSource媒体资源:

<ListBox ItemsSource="{Binding YourCollectionProperty}" />

当然,您需要声明YourCollectionProperty属性:

// Implement the INotifyPropertyChanged interface on this property:
public ObservableCollection<LearnedElement> YourCollectionProperty { get; set; }

最后,将您的项目直接添加到集合属性中:

...
YourCollectionProperty.Add(local:learnedElem);

您添加的每个项目都会显示在ListBox中。哦,最后一件事......声明你的DataTemplate这样,以便框架知道在看到你的对象时使用它:

<DataTemplate DataType="{x:Type LearnedElement}"> 
    ...
</DataTemplate>