【问题标题】:Get control properties from Grid.Resources in code behind在后面的代码中从 Grid.Resources 获取控件属性
【发布时间】:2013-07-02 05:23:19
【问题描述】:

开头是xaml代码:

<Grid.Resources>
            <DataTemplate x:Name="dataTemp" x:Key="dtKey">
                <WrapPanel Orientation="Horizontal" Name="mainWP">
                    <TextBlock Name="codeTB" FontSize="18" Width="200" Text="{Binding barcode}"></TextBlock>
(...)
               </WrapPanel>
            </DataTemplate>
        </Grid.Resources>

和带有数据模板的列表视图:

<ListView Name="testLV" Grid.Row="0" ItemTemplate="{StaticResource ResourceKey=dtKey}" >

        </ListView>

所以在后面的代码中,我想将 TextBlock 宽度更改为 this.width/5(因为另一台 PC 中的宽度可能不同),但因为它是 DataTemplate,所以我无权访问此控件。 我也试过 Width="{Binding Path=ActualWidth, ElementName=grid0}",但作为实际宽度,我需要像 ActualWidth/5 这样的东西,这不起作用

谢谢

【问题讨论】:

  • 好吧,假设您在Grid 中使用此DataTemplate,您为什么不相应地设置您的Grid.ColumnDefinitions,使其宽度为使用此@987654326 的列的“0.2*” @ 然后从您的Datatemplate 中删除固定宽度规格。使用TryFindResource(...) 只会让您的代码变得破旧,并在以后给您带来更多问题。您也可以使用转换器来执行“ActualWidth/5”,但请不要。以正确的方式使用 WPF(限制“幻数”固定大小规范),您将拥有更好的构建应用程序的体验。
  • 嗯,但我使用 DataTemplate 进行列表视图,我将编辑第一篇文章并显示
  • 不确定我是否理解你说的对:) 在 DataTemplate 中,我有 5 个 TextBlocks 的 wrappanel。所以现在我需要将 Listview 放入 Column - 好的。但是 WrapPanel 中的所有控件怎么样?我只想将整个宽度拆分为 5 个控件(TextBlocks),它们位于 Wrappanel 内。
  • 在这种情况下,当不使用 Grid.ColumnDefinition 时,只需将 ListView 放入 0.2* 的网格宽度。但我想在 DataTemplate 中声明 0.2* foreach 控件。所以我需要为 ListView 设置 100% 的宽度,并从 DataTemplate 中获取 foreach 控件,例如 ColumnDefinition 来定义它们的宽度
  • 现在我明白了。作为DataTemplate,我应该使用带有列定义的Grid而不是Wrappanel,对吗? ;) 好的,现在似乎一切正常,但是 0.2* 宽度计算为 20% ow 宽度?因为看起来不像:P

标签: wpf resources datatemplate


【解决方案1】:

当所有子元素的 DesiredWidth 都满足时,使用具有 5 列且每列具有 Width="0.2*"Grid 将正常工作(换句话说,当 Grid 的大小足够大以容纳具有相等的所有列时空间)。如果它不能做到这一点,布局的工作方式是修剪它可以修剪的元素,并为其他需要它的列提供额外的空间,从而覆盖过程中的Width="0.2*"

对于您希望将 5 列平均拆分的要求,只需使用 UniformGrid。这几乎不关心上述任何事情。

所以说这样的话:

<ListView Name="paragonLV" HorizontalContentAlignment="Stretch">
  <ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
      <Setter Property="Padding"
              Value="0" />
      <Setter Property="BorderThickness"
              Value="0" />
    </Style>
  </ListView.ItemContainerStyle>
  <ListView.ItemTemplate>
    <DataTemplate>
      <UniformGrid MaxWidth="{Binding RelativeSource={RelativeSource FindAncestor,
                                                                      AncestorType={x:Type ScrollViewer}},
                                      Path=ActualWidth}"
                    Columns="5">
        <UniformGrid.Resources>
          <Style TargetType="{x:Type TextBlock}">
            <Setter Property="TextTrimming"
                    Value="CharacterEllipsis" />
            <Setter Property="FontSize"
                    Value="18" />
            <Setter Property="HorizontalAlignment"
                    Value="Stretch" />
          </Style>
        </UniformGrid.Resources>
        <TextBlock Text="{Binding barCode}" />
        <TextBlock Text="{Binding nazwa}" />
        <TextBlock Text="{Binding jm}" />
        <TextBlock Text="{Binding ilosc}" />
        <TextBlock Text="{Binding cena}" />
      </UniformGrid>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

【讨论】:

    【解决方案2】:

    使用 Grid.Columndefination 格式化您的网格或使用 Ivaluconverter 类让我们看看值转换器的开发

    转换参数将您的计算参数, 你知道如何构造值转换器类

     public object Convert(object value, Type targetType, 
            object parameter, CultureInfo culture)
        {
            //value is grid actual width 
           // parameter = 5 is your calculated value
    
               return value / parameter;
        }
    

    【讨论】:

      猜你喜欢
      • 2011-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-29
      • 2016-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多