C# WPF文本块不显示自定义设置
我有一个程序,从erp文档处理源加载图片,并允许通过触摸或鼠标移动和调整大小。有一个叫做Picture的类:C# WPF文本块不显示自定义设置,c#,wpf,C#,Wpf,我有一个程序,从erp文档处理源加载图片,并允许通过触摸或鼠标移动和调整大小。有一个叫做Picture的类: using System.Windows; using System.Windows.Controls; namespace DocumentHandlingTouch { /// <summary> /// Interaction logic for Picture.xaml /// </summary> public part
using System.Windows;
using System.Windows.Controls;
namespace DocumentHandlingTouch
{
/// <summary>
/// Interaction logic for Picture.xaml
/// </summary>
public partial class Picture : UserControl
{
public Picture()
{
InitializeComponent();
DataContext = this;
}
public string ImagePath
{
get { return (string)GetValue(ImagePathProperty); }
set { SetValue(ImagePathProperty, value); }
}
public string ImageName
{
get { return (string)GetValue(ImageNameProperty); }
set { SetValue(ImageNameProperty, value); }
}
public static readonly DependencyProperty ImageNameProperty =
DependencyProperty.Register("ImageName", typeof(string), typeof(Picture), new UIPropertyMetadata(""));
// Using a DependencyProperty as the backing store for ImagePath. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ImagePathProperty =
DependencyProperty.Register("ImagePath", typeof(string), typeof(Picture), new UIPropertyMetadata(""));
public string OriginalImagePath
{
get { return (string)GetValue(OriginalImagePathProperty); }
set { SetValue(OriginalImagePathProperty, value); }
}
public static readonly DependencyProperty OriginalImagePathProperty =
DependencyProperty.Register("OriginalImagePath", typeof(string), typeof(Picture), new UIPropertyMetadata(""));
public double Angle
{
get { return (double)GetValue(AngleProperty); }
set { SetValue(AngleProperty, value); }
}
// Using a DependencyProperty as the backing store for Angle. This enables animation, styling, binding, etc...
public static readonly DependencyProperty AngleProperty =
DependencyProperty.Register("Angle", typeof(double), typeof(Picture), new UIPropertyMetadata(0.0));
public double ScaleX
{
get { return (double)GetValue(ScaleXProperty); }
set { SetValue(ScaleXProperty, value); }
}
// Using a DependencyProperty as the backing store for ScaleX. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ScaleXProperty =
DependencyProperty.Register("ScaleX", typeof(double), typeof(Picture), new UIPropertyMetadata(1.0));
public double ScaleY
{
get { return (double)GetValue(ScaleYProperty); }
set { SetValue(ScaleYProperty, value); }
}
// Using a DependencyProperty as the backing store for ScaleY. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ScaleYProperty =
DependencyProperty.Register("ScaleY", typeof(double), typeof(Picture), new UIPropertyMetadata(1.0));
public double X
{
get { return (double)GetValue(XProperty); }
set { SetValue(XProperty, value); }
}
// Using a DependencyProperty as the backing store for X. This enables animation, styling, binding, etc...
public static readonly DependencyProperty XProperty =
DependencyProperty.Register("X", typeof(double), typeof(Picture), new UIPropertyMetadata(0.0));
public double Y
{
get { return (double)GetValue(YProperty); }
set { SetValue(YProperty, value); }
}
// Using a DependencyProperty as the backing store for Y. This enables animation, styling, binding, etc...
public static readonly DependencyProperty YProperty =
DependencyProperty.Register("Y", typeof(double), typeof(Picture), new UIPropertyMetadata(0.0));
}
}
问题是textblock没有改变它的样式…它不是粗体的,它没有垂直放置在底部,它没有包装。它只是用一种朴素的旧字体与图片相呼应。
我做错了什么?
编辑我注释掉了文本块,图像路径仍然显示出来。显示的是ImagePath依赖项属性吗?我很困惑您忘记将textblock添加到画布:
_canvas.Children.Add(tb);
编辑
如果图片用户控件中有textblock,则不能在代码隐藏中创建新的文本块
在Picture.xaml中,您应该定义文本块的名称:
<TextBlock x:Name="TextBlock1 />
tb与图片一起添加:Dispatcher.Invoke((操作)(()=>\u canvas.Children.Add(p));我试着单独添加,结果是一样的。Textblock就在那里,只是在您的问题中的代码示例中没有格式化,您正在创建新的Textblock。您正在设置新文本块的属性,而不是图片中的文本块。
<TextBlock x:Name="TextBlock1 />
TextBlock tb = p.TextBlock1;