我有一个程序可以从erp文档处理源加载图片,并允许通过触摸或鼠标移动和调整它们的大小。有一个名为Picture的课程:
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));
}
}
这是加载图片的类。它使用图片下方的文本框显示文件名:
private void LoadPictures()
{
_canvas.Children.Clear();
double x = -10;
double y = 0;
foreach (DictionaryEntry filePath in files)
{
try
{
Picture p = new Picture();
ToolTip t = new ToolTip();
t.Content = filePath.Value;
p.ToolTip = t;
///////////////////Heres where the textblock is set////////////////
TextBlock tb = new TextBlock();
tb.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
tb.FontWeight = FontWeights.UltraBlack;
tb.TextWrapping = TextWrapping.WrapWithOverflow;
tb.Width = filePath.Value.ToString().ToString().Length * 12;// 125;
tb.Height = 25;
tb.Text = filePath.Value.ToString();
Canvas.SetTop(tb, y+ 25);
Canvas.SetLeft(tb, x + 25);
//////////////////////////////////////////////////////////////////
//External Program
if (Path.GetExtension(filePath.Key.ToString()) == ".pdf")
{
var path = new Uri("pack://application:,,,/Document%20Handling%20Viewer;component/Resources/pdf.png", UriKind.Absolute);
p.ImagePath = path.ToString();
p.OriginalImagePath = filePath.Key.ToString();
p.ImageName = filePath.Value.ToString();
}
else
{
p.ImagePath = filePath.Key.ToString();
p.OriginalImagePath = filePath.Key.ToString();
p.ImageName = filePath.Value.ToString();
}
p.Width = 100;
p.X = x;
p.Y = y;
x = x + p.Width + 5;
if (x + p.Width >= System.Windows.SystemParameters.PrimaryScreenWidth)
{
y = y + 150;
x = 0;
}
Dispatcher.Invoke((Action)(() => _canvas.Children.Add(p)));
}
catch (Exception ex)
{
MessageBox.Show("Error:" + ex.Message, "File Load Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
问题是文本块没有改变它的样式......它不是粗体,它没有垂直放置在底部而且它没有包裹。它只是用简单的旧字体拥抱图片。 我在这做错了什么?
编辑我注释掉了TextBlock,图像路径仍然显示出来。是否出现了ImagePath依赖属性?我很困惑
答案 0 :(得分:0)
您忘记将文字块添加到画布:
Visible=False
修改强>
如果您在Picture usercontrol中有文本块,则不得在后面创建新代码。
在Picture.xaml中,您应该定义文本块的名称:
_canvas.Children.Add(tb);
在c#代码替换行中: with:<TextBlock x:Name="TextBlock1 />
TextBlock tb = new TextBlock();