【问题标题】:WPF datatemplate ItemsControl specifying initial and final valuesWPF 数据模板 ItemsControl 指定初始值和最终值
【发布时间】:2010-09-30 02:59:23
【问题描述】:

我一直在 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>  

我是这样使用它的:

<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 , TextBox , TextBox ) **

使用代码隐藏很容易实现这一点,但我不想在我的代码隐藏文件中编写任何内容,我希望我的视图是干净的。我只想在 XAML 文件中编写所有内容有关如何执行此操作的示例说明将有很大帮助!

【问题讨论】:

    标签: .net wpf datatemplate itemscontrol


    【解决方案1】:

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

    【讨论】:

    • 谢谢德鲁,但我很抱歉我没有得到你。我应该在哪里包装 ItemsControl 以及应该在哪里放置触发器。我在我的问题中添加了更多关于我如何使用它的细节..
    • @Guru,我已经用一些代码更新了我的答案。希望这更清楚。
    • Yipie 非常感谢!!!它的工作精湛。我花了这么多小时......再次非常感谢你!!!!
    • 注意:如果您不熟悉它,则不是很明显,但“PreviousData”实际上是框架的一部分。文档在这里msdn.microsoft.com/en-us/library/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    • 2022-11-20
    • 1970-01-01
    • 2014-08-27
    • 1970-01-01
    • 2013-04-26
    相关资源
    最近更新 更多