Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何动态(通过代码)添加在WPF中的XAML中创建的自定义控件?_C#_Wpf_Xaml_User Controls - Fatal编程技术网

C# 如何动态(通过代码)添加在WPF中的XAML中创建的自定义控件?

C# 如何动态(通过代码)添加在WPF中的XAML中创建的自定义控件?,c#,wpf,xaml,user-controls,C#,Wpf,Xaml,User Controls,我已经通过visualstduio(和Blend)构建了一个XAML“模板”控件,当然是在XAML中。当我搜索如何将控件动态添加到界面中时,答案总是显示动态构建控件,然后添加它。我的问题是如何添加已创建的控件 总的来说,我对WPF和C非常陌生。也许我遗漏了什么。我开始思考,我可以在VisualStudio中用它的GUI创建界面,然后编写后端代码,但似乎还有一些我不理解的地方。我试图设计一个基本上是“搜索缩略图”的控件,或者换句话说,一个带有图像和文本框的彩色窗格。我将图像设置为nothing,将

我已经通过visualstduio(和Blend)构建了一个XAML“模板”控件,当然是在XAML中。当我搜索如何将控件动态添加到界面中时,答案总是显示动态构建控件,然后添加它。我的问题是如何添加已创建的控件

总的来说,我对WPF和C非常陌生。也许我遗漏了什么。我开始思考,我可以在VisualStudio中用它的GUI创建界面,然后编写后端代码,但似乎还有一些我不理解的地方。我试图设计一个基本上是“搜索缩略图”的控件,或者换句话说,一个带有图像和文本框的彩色窗格。我将图像设置为nothing,将框文本设置为null,认为可以在代码中更改这些值。我得到了一个搜索结果列表,并试图为每个结果添加一个缩略图控件,我假设它是一个包装(我不确定这是否正是我想要的,但我相信它是)

这是我的缩略图控件,它是XML

我预计会完全失败,但相反,我将控件添加到主窗格中,并假设其边距为0。不显示任何图像,也不更改任何文本


用户控件应公开如下依赖项属性:

public partial class SearchThumbnail : UserControl
{
    public SearchThumbnail()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty ImageProperty = DependencyProperty.Register(
        nameof(Image), typeof(ImageSource), typeof(SearchThumbnail));

    public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
        nameof(Text), typeof(string), typeof(SearchThumbnail));

    public ImageSource Image
    {
        get { return (ImageSource)GetValue(ImageProperty); }
        set { SetValue(ImageProperty, value); }
    }

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
}
请注意,您不能在其构造函数中显式设置UserControl的DataContext,否则依赖项属性的任何基于DataContext的标准绑定都无法工作

在其XAML中,您可以通过相对资源绑定等方式绑定到UserControls自己的属性:

<UserControl ...>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Image Grid.Row="0"
            Source="{Binding Image, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
        <TextBlock Grid.Row="1"
            Text="{Binding Text, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
    </Grid>
</UserControl>

不要向我们显示您的XAML的图像。这是不可读的,而且动画很烦人。相反,将XAML添加到您的问题中,就像您的代码一样。@Clemens没有考虑将其添加到问题中。修好了,谢谢。工作得很好。非常感谢。对这个框架的使用有着深刻的见解。进一步阅读的一个好的起点是
                    // Getting a compatible Image object for the SearchThumbnail image Pane. Code from another stack overflow thread
                    var imgUrl = new Uri(thumbnail);
                    var imageData = new WebClient().DownloadData(imgUrl);

                    var bitmapImage = new BitmapImage { CacheOption = BitmapCacheOption.OnLoad };
                    bitmapImage.BeginInit();
                    bitmapImage.StreamSource = new MemoryStream(imageData);
                    bitmapImage.EndInit();

                    // OBVIOUSLY FAILED LAST ATTEMPT HERE.
                    var thumb = new SearchThumbnail()
                    {
                    ThumbnailTxt = title,
                    ThumbnailPath = bitmapImage
                    };
                    this.wrapPanel.Children.Add(thumb);
public partial class SearchThumbnail : UserControl
{
    public SearchThumbnail()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty ImageProperty = DependencyProperty.Register(
        nameof(Image), typeof(ImageSource), typeof(SearchThumbnail));

    public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
        nameof(Text), typeof(string), typeof(SearchThumbnail));

    public ImageSource Image
    {
        get { return (ImageSource)GetValue(ImageProperty); }
        set { SetValue(ImageProperty, value); }
    }

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
}
<UserControl ...>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Image Grid.Row="0"
            Source="{Binding Image, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
        <TextBlock Grid.Row="1"
            Text="{Binding Text, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
    </Grid>
</UserControl>
var bitmapImage = new BitmapImage(imgUrl);