【问题标题】:Binding to ActualWidth in changing collection在更改集合中绑定到 ActualWidth
【发布时间】:2016-05-09 09:25:37
【问题描述】:

我有一个FlipView 控制女巫,由图像和关于图像的文本组成。我希望文本与图像的宽度相同。有些图片的尺寸与其他图片不同。
这是我的 XAML 代码:

<FlipView x:Name="flipView"  HorizontalContentAlignment="Center" VerticalContentAlignment="Center" ItemsSource="{Binding ImagesWithDescriptions}">
    <FlipView.ItemTemplate>
        <DataTemplate>
            <Grid x:Name="grid" Tapped="flipView_Tapped">
                <Grid.RowDefinitions>
                    <RowDefinition Height="3*"/>
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Image x:Name="image" Grid.RowSpan="2" Source="{Binding Image}"></Image>
                <Grid x:Name="textGrid" Grid.Row="1">
                    <Grid.Background>
                        <SolidColorBrush Color="Black" Opacity="0.5"/>
                    </Grid.Background>
                    <TextBlock Foreground="White" HorizontalAlignment="Left" FontSize="20" Text="{Binding Description}"/>
                </Grid>
            </Grid>
        </DataTemplate>
    </FlipView.ItemTemplate>
</FlipViewControl>

如果我尝试将文本绑定到图像的ActualWidth,它总是返回 0。有没有办法做到这一点?

编辑:
它看起来像这样:

(-----------------FlipView 宽度----------------------) -------------------------------------------------- - - - F | |这是图像。它的高度是| |l | |等于 FlipView 的高度,| |我 | |但宽度取决于图片的| |p | |比率,可能在 | 上有所不同|V | |一些图片。 | |我 | | | |e | | | |w | | | | | | | |h | | | |e |---------|------------------|------- -----|我 |这是 |现在网格名为“textGrid”的位置(它是 |g |宽度与 FlipView 的相同) | |h -------------------------------------------------- ------t

但我希望名为“textGrid”的Grid 具有与 Image 相同的宽度。
绑定&lt;Grid x:Name="textGrid" Width="{Binding ElementName=image, Path=ActualWidth}"/&gt; 导致Grid 的宽度始终等于0。Image Loaded 事件也将ActualWidth 返回为0。

【问题讨论】:

  • Image 将为 ActualWidth 和 ActualHeight 返回 0,直到加载内部图像。之后它会返回非零值吗?
  • @Jai 我应该使用什么事件来检查?
  • 图片应该有加载事件
  • @XAMlMAX 图像的加载事件返回 0。
  • “我希望文本与图像的宽度相同”,这到底是什么意思?当前,您的 TextBlock 左对齐并具有固定的字体大小。其父 Grid(具有半透明黑色背景)应与外部 Grid 按布局具有相同的宽度。那么究竟是什么问题呢?

标签: c# wpf xaml binding actualwidth


【解决方案1】:
<FlipView x:Name="flipView" ItemsSource="{Binding ListTest}" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" >
            <FlipView.ItemTemplate>
                <DataTemplate>
                    <Grid x:Name="grid" Background="Blue" >
                        <Grid.RowDefinitions>
                            <RowDefinition Height="3*"/>
                            <RowDefinition />
                        </Grid.RowDefinitions>
                        <Grid Grid.RowSpan="2" x:Name="image">
                            <Image  Stretch="Uniform" SizeChanged="Image_SizeChanged"  Source="{Binding Url}" ></Image>
                        </Grid>
                        <Grid   x:Name="textblockgrid" Grid.Row="1" Background="Gray">
                            <TextBlock Foreground="White" TextWrapping="Wrap"  HorizontalAlignment="Left" FontSize="20" Text="TEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX trysffffffffffffff sdfffffffffffff dfhdffffffffffffff dfhhhhhhhhX"/>
                        </Grid>
                    </Grid>
                </DataTemplate>


                </FlipView.ItemTemplate>

        </FlipView>


    private void Image_SizeChanged(object sender, SizeChangedEventArgs e)
    {
      FlipViewItem item = (FlipViewItem) flipView.ContainerFromItem((sender as Image).DataContext);
      var text =  FindElementInVisualTree<TextBlock>(item);
        (text.Parent as Grid).Width = (sender as Image).ActualWidth;
    }
    private T FindElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
    {
        var count = VisualTreeHelper.GetChildrenCount(parentElement);
        if (count == 0) return null;

        for (int i = 0; i < count; i++)
        {
            var child = VisualTreeHelper.GetChild(parentElement, i);
            if (child != null && child is T)
                return (T)child;
            else
            {
                var result = FindElementInVisualTree<T>(child);
                if (result != null)
                    return result;
            }
        }
        return null;
    }

【讨论】:

    猜你喜欢
    • 2010-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多