[英]WPF TextBlock not showing customizations
我有一個程序可以從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不會改變其樣式...它不是粗體的,它不是垂直放置在底部的,並且不會包裝。 它只是以普通的舊字體擁抱圖片。 我在這里做錯了什么?
編輯我注釋掉了TextBlock,圖像路徑仍然顯示。 顯示的是ImagePath依賴項屬性嗎? 我糊塗了
您忘記將textblock添加到畫布:
_canvas.Children.Add(tb);
編輯
如果Picture用戶控件中有文本塊,則不得在后面的代碼中創建新的塊。
在Picture.xaml中,您應該定義文本塊的名稱:
<TextBlock x:Name="TextBlock1 />
在C#代碼替換行中:
與: TextBlock tb = new TextBlock();
TextBlock tb = p.TextBlock1;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.