.net - WPF 数据模板 ItemsControl 指定初始值和最终值

标签 .net wpf datatemplate itemscontrol

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

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

我写了一个像这样的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>  

我这样使用它:

<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>  

但问题是,我必须在第一个文本框之前显示 "(" ,在最后一个文本框之后显示 ")""," 在文本框之间。
我必须显示类似这样的内容::::>    ** ( TextBox , TextBox , TextBox ) **

使用代码隐藏实现此目的很容易,但我不想在代码隐藏文件中写入任何内容,并且我希望我的 View 是干净的。我只想将所有内容写入 XAML 文件中 有关如何执行此操作的示例说明将非常有帮助!

最佳答案

用某种将括号放在两侧的控件(可能是停靠面板)包裹 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>

关于.net - WPF 数据模板 ItemsControl 指定初始值和最终值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3827381/

相关文章:

c# - 窗口级依赖属性( View 的代码在后面),它可以绑定(bind)到 View 的 XAML 中吗?

c# - 使用数据绑定(bind)处理样式

WPF TreeView : Using ItemTemplateSelector and ItemContainerStyle Not Working At Same Time

xamarin.forms - 如何从 Xamarin 表单中的 DataTemplate 条目 TextChanged 事件调用命令?

wpf - .Net 4.5 中事件的标记扩展

c# - 事务必须在 SQL Server 的存储过程中吗?

c# - 列表+详细信息 - 最佳方法?

.net - 在生产中使用 Entity Framework (代码优先)迁移

.net - 使用 [Type].Parse 还是 C[Type] 更好

c# - 如何通知 View 命令成功