Tag: 文本块

嵌套的TextBlocks和Hyperlinks,你如何在C#中复制这个XAML?

我有这个XAML: This is some Blue text. This is some Green text following the Blue text. 我想知道如何在C#中以程序方式复制它。 我知道如何在C#中创建TextBlock ,例如: TextBlock tb = new TextBlock(); tb.Text=”Some text” 我可以在C#中的一个面板中放置多个TextBlock 。 但我不知道如何将TextBlock放入其他TextBlock ,而将TextBlock放入Hyperlink放入TextBlock 。 是不是会自动创建一些容器对象和额外的TextBlock对象? 或者TextBlock是否有一些方法/属性允许它包含其他项? 其他相关问题: 1.将什么是Click()事件添加到Hyperlink的最佳方法是什么? 2.有没有办法让蓝色文字更干净地包裹? 在上面的XAML中,只要最右边的单词需要换行,就会包裹整个蓝色文本块。 感谢您提供的任何照明。

Wpf将TextBlock绑定到App Property的成员

在我的WPF应用程序中,我希望全局使用对象“CaseDetails”,即所有窗口和用户控件。 CaseDetails实现了INotifyPropertyChanged,并具有属性CaseName。 public class CaseDetails : INotifyPropertyChanged { private string caseName, path, outputPath, inputPath; public CaseDetails() { } public string CaseName { get { return caseName; } set { if (caseName != value) { caseName = value; SetPaths(); OnPropertyChanged(“CaseName”); } } } protected virtual void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != […]

通过C#代码创建DataGridTemplateColumn

我有一个我创建的动态Datagrid。 我通过后面的代码为它创建每一列。 我在列上遇到麻烦,我希望在不编辑时显示在文本块中,而是在编辑时作为combobox显示。 我有一个ObservableCollection of Transactions。 每个交易都有一个名为“账户”的类型。 这是我到目前为止: private DataGridTemplateColumn GetAccountColumn() { // Create The Column DataGridTemplateColumn accountColumn = new DataGridTemplateColumn(); accountColumn.Header = “Account”; Binding bind = new Binding(“Account”); bind.Mode = BindingMode.TwoWay; // Create the TextBlock FrameworkElementFactory textFactory = new FrameworkElementFactory(typeof(TextBlock)); textFactory.SetBinding(TextBlock.TextProperty, bind); DataTemplate textTemplate = new DataTemplate(); textTemplate.VisualTree = textFactory; // Create the ComboBox […]

XAML中的WPF文本块绑定

我正在更新一些现有的WPF代码,我的应用程序有许多像这样定义的文本块: 在这种情况下,“PropertyA”是我的业务类对象的属性,定义如下: public class MyBusinessObject : INotifyPropertyChanged { private void OnPropertyChanged(PropertyChangedEventArgs e) { if (PropertyChanged != null) { PropertyChanged(this, e); } } private string _propertyA; public string PropertyA { get { return _propertyA; } set { if (_propertyA == value) { return; } _propertyA = value; OnPropertyChanged(new PropertyChangedEventArgs(“PropertyA”)); } } // my business object also […]

TextBlock中的绑定在WPF中不起作用

我想在我的类中动态更改TextBlock文本。 XAML代码 : C# : string footerMainMenuText = “Setting”; Binding set = new Binding(“”); set.Mode = BindingMode.OneWay; set.Source = footerMainMenuText; Footer_text.DataContext = footerMainMenuText; Footer_text.SetBinding(TextBlock.TextProperty, set); 我检查了最后一行,并正确设置了Footer_text.Text 。 ( Footer_text.Text=”Setting” ),但我的应用程序中的TextBlock没有显示“Setting”。 这里有什么问题?

是否期望这种慢速WPF TextBlock性能?

我正在做一些基准测试,以确定我是否可以将WPF用于新产品。 然而,早期的表现结果令人失望。 我制作了一个快速的应用程序,它使用数据绑定每100毫秒在列表框内显示一堆随机文本,它占用了大约15%的CPU。 所以我创建了另一个跳过数据绑定/数据模板方案的快速应用程序,除了每100毫秒更新一个ListBox内的10个TextBlocks之外什么都不做(实际产品不需要100毫秒更新,更像是500毫秒最多,但是这是一个压力测试)。 我仍然看到大约5-10%的CPU使用率。 为什么这么高? 是因为所有的垃圾串吗? 这是不使用绑定的版本的XAML: 这是背后的代码: public partial class Window1 : Window { private int _count = 0; public Window1() { InitializeComponent(); } private void OnLoad(object sender, RoutedEventArgs e) { var t = new DispatcherTimer(TimeSpan.FromSeconds(0.1), DispatcherPriority.Normal, UpdateNumerics, Dispatcher); t.Start(); } private void UpdateNumerics(object sender, EventArgs e) { ++_count; foreach (object textBlock in […]

以编程方式在文本之间创建带有超链接的文本块

在XAML中,我有以下代码: click here please 现在我想摆脱整个TextBlock XAML并以编程方式添加该位。 我创建TextBlock没有问题,将Text属性设置为’click please’并向TextBlock.Content添加超链接。 但是如何在“点击”和“请”之间定位超链接? 如何将超链接的文本设置为“here”? 我没有太大的进展,到目前为止,我得到的是: label2.Content = new TextBlock() { Text = “click please” }; //(label2.Content as TextBlock).Content does not exist? //and even if it does.. how do I squeeze the hyperlink in between the text?