【问题标题】:WPF Custom Control Work by itself but not as part of another controlWPF 自定义控件单独工作,但不作为另一个控件的一部分
【发布时间】:2014-10-01 10:22:28
【问题描述】:

我有一个用于显示图像的 WPF 自定义控件。

有一个列表视图绑定到可观察的数据库实体集合,随后通过值转换器将其转换为图像。

当我将控件拖到 Windows 窗体项目(使用 WPF 主机控件)上时,它在分配列表后面的可观察集合时工作得很好。我已经对此进行了测试,它可以正确更新并完成我需要的一切。

但是

我想要三个这样的控件来显示相关的图像,所以我创建了第二个控件,它将三个原始控件简单地分组到一个堆栈面板中。

我为每个更新 images 属性的方法创建了一个方法。

 public void ChangeSearchResults(List<ItemImage> items)
 {
      SearchResultsImageViewer.ItemImages = new ObservableCollection<ItemImage>(items);
 }

但是我根本无法显示图像。

直接查看控件和查看控件作为子控件似乎是有区别的。

我很确定它不是 winforms 中的元素宿主,因为控件本身运行良好。

有什么我没有意识到的吗?

这是列表视图的 Xaml

        <!--  Sets the template for the data to be displayed  -->
        <ListView.ItemTemplate>

            <DataTemplate>
                    <StackPanel>
                        <!--  Defines the actual image being displayed  -->
                        <Image x:Name="ItemImageControl"
                               Width="100"
                               Height="200"
                               Margin="1"
                               HorizontalAlignment="Stretch"
                               VerticalAlignment="Stretch"
                               Cursor="Hand"
                               Source="{Binding .,
                                                Converter={StaticResource imageConverter}}" />

                        <TextBlock HorizontalAlignment="Center"
                                   FontWeight="Bold"
                                   Text="{Binding .,
                                                  Converter={StaticResource groupNameConverter}}" />
                    </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

EDIT - 这是用户控件 XAML

<UserControl x:Class="Project.CustomControls.ctrlImageCollection"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:CustomControls="clr-namespace:Project.CustomControls"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d">
        <StackPanel>
            <CustomControls:ctrlImageViewer x:Name="ShortlistImageViewer" />
            <CustomControls:ctrlImageViewer x:Name="SearchResultsImageViewer" />
            <CustomControls:ctrlImageViewer x:Name="GroupImageViewer" />
        </StackPanel>
</UserControl>

【问题讨论】:

  • 你是如何区分两个不同的图像控件的!!!
  • 我给他们起了不同的名字?

标签: c# wpf wpf-controls


【解决方案1】:

随着使用

...
Source={Binding ., Converter={StaticResource imageConverter}}" />

您绑定到当前的 UserControl。当您单独使用 ListView 时,它会绑定到包含 ListView 的 UserControl 并且可以工作。

如果您将 ListView UserControl 放入另一个 UserControl,它的值将绑定到其父 UserControl,而不是“拥有”UserControl。

试试这个:

转到您的 ListView UserControl xaml.cs 并将 DataContext 设置为自身。

//...
DataContext = this;
//...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多