【问题标题】:Multiple Children in one DataTemplate一个 DataTemplate 中的多个子项
【发布时间】:2020-06-14 20:55:37
【问题描述】:

如何向 ItemsControl.ItemTemplate 内的 DataTemplate 添加两个元素? 我需要这样的东西:

...
<ItemsControl.ItemTemplate>
  <DataTemplate>
    <Grid/>
    <TextBlock/>
  </DataTemplate>
<ItemsControl.ItemTemplate>
...

我想在我现有的 DataTemplate 中添加一个 Textblock,但是这个 DataTemplate 已经包含一个 Grid。

我想要达到的目标: 当代表我的行号的定义时,我想向我的网格添加一个文本块。 MainViewModel 中有一个 ObservableCollection LineNumberList,它应该绑定到这个 Textblock。

我的主视图:

<ItemsControl ItemsSource="{Binding MachineList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Grid ShowGridLines="False"
                              gridhelper:GridHelpers.RowCount="{Binding RowCount}"
                              gridhelper:GridHelpers.ColumnCount="{Binding ColumnCount}"
                              gridhelper:GridHelpers.StarColumns="0,1,2,3,4">
                        </Grid>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid Width="200"
                              Height="200">
                            <Image  Style="{StaticResource ImageMachine}" />
                            <Grid ShowGridLines="True"
                                  Style="{StaticResource SubGridMachine}" />
                        </Grid>

                        <TextBlock "WHICH I NEED HERE" 
                            Grid.Row="{Binding LineNumberList}"
                            Grid.Column"0"/>

                    </DataTemplate>
                </ItemsControl.ItemTemplate>
                <ItemsControl.ItemContainerStyle>
                    <Style>
                        <Setter Property="Grid.Row"
                                Value="{Binding LineNumber}" />
                        <Setter Property="Grid.Column"
                                Value="{Binding StationNumber}" />
                    </Style>
                </ItemsControl.ItemContainerStyle>
            </ItemsControl>

我的 MainViewModel:

 #region Variables Lists
    public ObservableCollection<Machine> MachineList { get; set; }
    public ObservableCollection<Head> HeadList { get; set; }
    public ObservableCollection<int> LineNumberList { get; set; }
    #endregion

    public int RowCount { get; set; } = 10;
    public int ColumnCount { get; set; } = 5;
    public CreateMachineCommand CreateMachineCommand { get; set; }
    public DeleteMachineCommand DeleteMachineCommand { get; set; }




    public MainViewModel()
    {
        MachineList = new ObservableCollection<Machine>();
        HeadList = new ObservableCollection<Head>();
        LineNumberList = new ObservableCollection<int>();
        CreateMachineCommand = new CreateMachineCommand(this);
        DeleteMachineCommand = new DeleteMachineCommand(this);
        ReadMachineFromDB();
    }

【问题讨论】:

    标签: wpf xaml mvvm


    【解决方案1】:

    也许使用 StackPanel 并在其中包含您的 Grid 和 TextBlock。

    <ItemsControl>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <Grid Width="200"
                          Height="200">
                        <Image  Style="{StaticResource ImageMachine}" />
                        <Grid ShowGridLines="True"
                                Style="{StaticResource SubGridMachine}" />
                    </Grid>
    
                    <TextBlock "WHICH I NEED HERE" 
                        Grid.Row="{Binding LineNumberList}"
                        Grid.Column"0"/>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    

    【讨论】:

    • 这不起作用。文本框与网格位于同一单元格中。但文本框需要始终在 Grid.Column="0"
    猜你喜欢
    • 2011-03-05
    • 1970-01-01
    • 2014-11-26
    • 2011-03-02
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多