构建UserControl以动态显示文本和图像

时间:2015-10-14 16:44:42

标签: c# wpf user-controls desktop-application

我正在构建一个测验可再发行桌面应用程序。问题可以是文本或图像,也可以是两者。 有人建议我构建一个文本和图像动态用户控件来显示问题。我已经全面搜索但无法找到任何教程来展示如何构建这样的用户控件。

我被引用http://www.akadia.com/services/dotnet_user_controls.htmlhttp://en.csharp-online.net/Create_List_Controls,https://msdn.microsoft.com/en-us/library/aa302342.aspx 但它有助于

1 个答案:

答案 0 :(得分:1)

据我所知,您需要一些可以显示动态内容(文本/图像)的控件。我建议你使用内容控件根据它的当前数据上下文选择其内容DataTemplate。 我将在几分钟内完成整个解决方案。

        <ContentControl Content="{Binding CurrentControlContent.Content}">
            <ContentControl.Resources>
                <DataTemplate DataType="{x:Type soSandBoxListView:SingleTextModel}">
                    <TextBlock Text="{Binding SingleModelContent}" Background="Tan"></TextBlock>
                </DataTemplate>
                <DataTemplate DataType="{x:Type soSandBoxListView:MultipleTextModel}">
                    <StackPanel>
                        <TextBlock Text="{Binding FirstName}" Background="Yellow"></TextBlock>
                        <TextBlock Text="{Binding LastName}" Background="Red"></TextBlock>
                        <!--use the binding to your picture presentation in model-->
                        <Image Source="Resources/yotveta.jpg" Stretch="None"></Image>
                    </StackPanel>
                </DataTemplate>
            </ContentControl.Resources>
        </ContentControl>

此致

相关问题