Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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# 将项目动态添加到缩略图_C#_Wpf_Xaml - Fatal编程技术网

C# 将项目动态添加到缩略图

C# 将项目动态添加到缩略图,c#,wpf,xaml,C#,Wpf,Xaml,我动态创建了两个拇指,看起来像矩形,并处理拖放事件,以便它们可以在画布中移动。稍后,当我按下UI上的某个按钮时,我想向画布中的每个拇指动态添加一些字符串。有办法吗。请帮忙 Xaml: 代码隐藏: private void CreateUIShapes(int numberOfWindows, List<Dimensions> dimensions) { Thumb th = null; for (int i = 0;

我动态创建了两个拇指,看起来像矩形,并处理拖放事件,以便它们可以在画布中移动。稍后,当我按下UI上的某个按钮时,我想向画布中的每个拇指动态添加一些字符串。有办法吗。请帮忙

Xaml:

代码隐藏:

 private void CreateUIShapes(int numberOfWindows, List<Dimensions> dimensions)
        {
            Thumb th = null;
            for (int i = 0; i < numberOfWindows; i++)
            {
                th = new Thumb();
                th.Name = i.ToString();
                var item = dimensions[i];
                th.Width = item.Width;
                th.Height = item.Height;
                th.Foreground = new SolidColorBrush(Windows.UI.Colors.Transparent);
                th.BorderBrush = item.BorderColor;
                th.BorderThickness = new Thickness(3);
                th.DragDelta += (sender, e) => Th_DragDelta(sender, e, dimensions);
                th.DragCompleted += (sender, e) => Th_DragCompleted(sender, e, item.IsImageRotated);
                //RotateWindowsByAngle(90, th, dimensions, i);
                Canvas.SetLeft(th, item.left);
                Canvas.SetTop(th, item.Top);
                cnvBarCodeImage.Children.Add(th);
            }
        }

        private void BtnScan_Click(object sender, RoutedEventArgs e)
        {
            //How can I add some text to the Thumb controls at this point. There is no Children Property.

        }
由于Thumb不是ContentControl,因此必须为其提供自定义控件模板,以便向其添加文本

下面是一个简单的示例,演示如何在代码中执行此操作:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Markup;
using System.Windows.Media;

namespace WpfApp4
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var thumb = new Thumb
            {
                Width = 100, 
                Height = 50, 
                Background = new SolidColorBrush(Colors.Red), 
                Foreground = new SolidColorBrush(Colors.White), 
                Template = GetThumbTemplate("")
            };

            Canvas.SetLeft(thumb, 100);
            Canvas.SetTop(thumb, 100);

            RootCanvas.Children.Add(thumb);

            thumb.Template = GetThumbTemplate("Hello world!");
        }

        private ControlTemplate GetThumbTemplate(string text)
        {
            var template = "<ControlTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" TargetType=\"Thumb\">" +
                           "<Border Background=\"{TemplateBinding Background}\">" +
                           "<TextBlock VerticalAlignment=\"Center\" HorizontalAlignment=\"Center\" Text=\"" + text + "\" />" +
                           "</Border>" +
                           "</ControlTemplate>";

            return XamlReader.Parse(template) as ControlTemplate;
        }
    }
}
当我创建Thumb时,我使用GetThumbTemplate设置模板,并传入一个空字符串。GetThumbTemplate创建一个带有边框和文本块的简单控件模板。然后解析XAML并返回它

将其添加到画布后,使用GetThumbTemplate方法更新Thumb的模板,并传入要显示的字符串


我希望这有帮助

谢谢你@Keithernet的帮助。问题是,当我最初说Template=GetThumbTemplate时,我并没有看到任何Thumb控件显示在UI上。现在,当我在扫描按钮单击处放置一个断点时,我可以看到一些模板已经应用于Thumb控件。是否有一种方法可以将元素添加到现有模板中。谢谢,这给了我一个想法。我可以试着把它修好。
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Markup;
using System.Windows.Media;

namespace WpfApp4
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var thumb = new Thumb
            {
                Width = 100, 
                Height = 50, 
                Background = new SolidColorBrush(Colors.Red), 
                Foreground = new SolidColorBrush(Colors.White), 
                Template = GetThumbTemplate("")
            };

            Canvas.SetLeft(thumb, 100);
            Canvas.SetTop(thumb, 100);

            RootCanvas.Children.Add(thumb);

            thumb.Template = GetThumbTemplate("Hello world!");
        }

        private ControlTemplate GetThumbTemplate(string text)
        {
            var template = "<ControlTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" TargetType=\"Thumb\">" +
                           "<Border Background=\"{TemplateBinding Background}\">" +
                           "<TextBlock VerticalAlignment=\"Center\" HorizontalAlignment=\"Center\" Text=\"" + text + "\" />" +
                           "</Border>" +
                           "</ControlTemplate>";

            return XamlReader.Parse(template) as ControlTemplate;
        }
    }
}