WPF datatemplate ItemsControl指定初始值和最终值

Gur*_*ruC 2 .net wpf datatemplate itemscontrol

我一直在WPF中使用ItemsControl来保持安静.我正在使用MVVM来开发我的应用程序.

现在我遇到了一个问题.我有一个要求,根据IList <>中的值的数量,我必须显示之间用逗号分隔的TextBox的数量.我编写了一个ItemsControl,它迭代绑定的IList <>并根据元素的数量显示TextBoxes.

我写过像这样的DataTemplate

<DataTemplate x:Key="ParameterDisplay1">  
    <ItemsControl ItemsSource="{Binding Parameters}">  
        <ItemsControl.ItemsPanel>  
            <ItemsPanelTemplate>  
                <StackPanel Orientation="Horizontal"></StackPanel>  
            </ItemsPanelTemplate>  
        </ItemsControl.ItemsPanel>  
        <ItemsControl.ItemTemplate>  
            <DataTemplate>  
                <StackPanel Orientation="Horizontal" >  
                    <TextBox  Width="40" Height="auto" Margin="2" Text="{Binding ProcessData}"></TextBox>  
                    <TextBlock Text=" , " Width="auto" Height="auto" Margin="2" FontSize="15"></TextBlock>  
                </StackPanel>  
            </DataTemplate>  
        </ItemsControl.ItemTemplate>  
     </ItemsControl>  
</DataTemplate>  
Run Code Online (Sandbox Code Playgroud)

我这样使用它:

<dg:DataGrid x:Name="DataGrid_Standard" toGenerateColumns="False">
    <dg:DataGrid.Columns>  
        <dg:DataGridTemplateColumn Header="Parameters" Width="SizeToCells" IsReadOnly="True"  
            CellTemplateSelector="{StaticResource paramTemplateSelector}">
        </dg:DataGridTemplateColumn> 
    </dg:DataGrid.Columns>  
</dg:DataGrid>  
Run Code Online (Sandbox Code Playgroud)

但问题是,我必须在TextBox之间显示"("在第一个TextBox之后的"(" )和最后一个TextBox之后的",".
我必须显示类似这样的东西::::>**(TextBox,TextBox,TextBox)**

使用代码隐藏实现这一点很容易,但我不想在我的代码隐藏文件中写任何东西,我希望我的View是干净的.我想在XAML文件中编写所有内容关于如何执行此操作的示例说明将非常有用!

Dre*_*kes 5

使用某种控件包装ItemsControl,将括号放在任一侧(可能是停靠面板).

然后,在项目前面显示的项目模板中添加逗号.使用具有"先前数据"类型的相对源的数据触发器,以便当前一项为空时,隐藏逗号(这样您将看不到第一项的逗号).

编辑

这是一些显示我的意思的代码.我没有您的数据类型或网格组件,因此它可能不太正确.然而,它突出了我提到的技术.

<DataTemplate x:Key="ParameterDisplay1">
  <StackPanel Orientation="Horizontal">
    <TextBlock>(</TextBlock>
    <ItemsControl ItemsSource="{Binding Parameters}">
      <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
          <StackPanel Orientation="Horizontal"></StackPanel>
        </ItemsPanelTemplate>
      </ItemsControl.ItemsPanel>
      <ItemsControl.ItemTemplate>
        <DataTemplate>
          <StackPanel Orientation="Horizontal" >
            <TextBlock Text=", " Name="Comma"></TextBlock>
            <TextBox Width="40" Text="{Binding ProcessData}"></TextBox>
          </StackPanel>
          <!-- Make a trigger that hides the comma for the first item -->
          <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource PreviousData}}" Value="{x:Null}">
              <Setter TargetName="Comma" Property="Visibility" Value="Collapsed" />
            </DataTrigger>
          </DataTemplate.Triggers>
        </DataTemplate>
      </ItemsControl.ItemTemplate>
    </ItemsControl>
    <TextBlock>)</TextBlock>
  </StackPanel>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)