如何在 WPF 中创建可重用的文本块绑定
本文关键字:文本 绑定 WPF 创建 | 更新日期: 2023-09-27 18:17:25
我一直在自学WPF,但我仍在学习基本的概念和术语。因此,如果这个问题的标题措辞不正确,请原谅我。
我有以下 XAML,它是绑定到类型 ViewModelBase
对象的HierarchicalDataTemplate
的一部分:
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding PrefixStyle.Text}"
FontWeight="{Binding PrefixStyle.Weight}"
Foreground ="{Binding PrefixStyle.Foreground}"
Margin="0,0,3,0"/>
<TextBlock Text="{Binding ValueStyle.Text}"
FontWeight="{Binding ValueStyle.Weight}"
Foreground ="{Binding ValueStyle.Foreground}"
Margin="0,0,3,0"/>
<TextBlock Text="{Binding SuffixStyle.Text}"
FontWeight="{Binding SuffixStyle.Weight}"
Foreground ="{Binding SuffixStyle.Foreground}"
Margin="0,0,3,0"/>
...
</StackPanel>
ViewModelBase
具有 XAML 中引用的相应属性:
public TextBlockStyle PrefixStyle...
public TextBlockStyle ValueStyle...
public TextBlockStyle SuffixStyle...
public class TextBlockStyle : INotifyPropertyChanged
{
public string Text...
public FontWieght Weight...
public Brush Foreground
}
如何将TextBlock
的绑定定义为在 XAML 中仅TextBlockStyle
一次,并避免像上面那样显式绑定每个TextBlock
属性?所以我可以为每个TextBlock
只有一行:
<StackPanel Orientation="Horizontal">
<TextBlock Source="{Binding PrefixStyle}" />
<TextBlock Source="{Binding ValueStyle}" />
<TextBlock Source="{Binding SuffixStyle}" />
...
</StackPanel>
我只是不知道从哪里开始。我需要对 TextBlock 进行子类化吗?使用绑定组?
这一定是程序员遇到的常见问题 - 所以如果以前有人问过这个问题,我深表歉意。我尝试过搜索,但我对 WPF 很陌生,我不知道如何正确表达我的问题。
使用隐式样式在整个应用程序中设置"全局"样式。 这通常在ResourceDictionary
内的App.xaml
文件中完成。
<Style TargetType="{x:Type TextBlock}"
BasedOn="{StaticResource TextBlockStyle}">
<Setter Property="FontWeight"
Value="Bold">
</Setter>
<Setter Property="Foreground"
Value="Red">
</Setter>
</Style>
如果您继续使用您遵循的方法(我不会(,您可以将您的 setter 更改为以下内容:
<Setter Property="FontWeight"
Value="{Binding Weight}">
</Setter>
然后,应用程序中的所有TextBlock
都将使用该样式,除非您定义更本地的显式/隐式样式。
编辑以详细说明注释:
我也在使用TreeView
和HierarchicalDataTemplate
. 以 MVVM 的方式思考,TreeView
中的每个项目(父/子项(都应该代表某种模型。 例如,想想Windows资源管理器。 其中的每一项都是 MVVM 世界中的Folder
或Drive
。 Folder
和Drive
不会有不同的字体粗细/颜色/大小特征,因为这都是与视图相关的。
在我们的示例中,您将有如下所示的内容:
public class BaseItem : ViewModel
{
public ObservableCollection<BaseItem> Children { .... }
public bool IsSelected { .... }
public string Title { .... }
}
由于一个文件夹可以容纳更多文件夹,而驱动器可以容纳文件夹,因此您可以像这样:
public class DriveVM : BaseItem { }
public class FolderVM : BaseItem { }
您将在DriveVM
内部Children.Add(new FolderVM(folder));
执行的所有操作,这将在驱动器下显示一个文件夹。 问题是这可能会变得非常复杂。 简而言之,我认为继承是使用TreeView
的关键。
另一种选择是这样的:
<Style x:Name="PrefixTextBlockStyle"
TargetType="{x:Type TextBox}">
<Setter Property="FontWeight"
Value="Bold" />
<Setter Property="Foreground"
Value="Red" />
<Setter Property="Text"
Value="{Binding Text}"
<Setter Property="Margin"
Value="0 0 3 0" />
</Style>
<Style x:Name="SuffixTextBlockStyle"
TargetType="{x:Type TextBox}">
<Setter Property="FontWeight"
Value="Italic" />
<Setter Property="Foreground"
Value="Orange" />
<Setter Property="Text"
Value="{Binding Text}" />
<Setter Property="Margin"
Value="0 0 3 0" />
</Style>
然后在您的HierarchicalDataTemplate
中执行以下操作:
<StackPanel Orientation="Horizontal">
<TextBlock DataContext="{Binding Prefix}"
Style="{StaticResource PrefixTextBlockStyle} ">
....
</StackPanel>
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
<Style TargetType="TextBlock">
<Setter Property="Text" Value="{Binding Text}" />
<Setter Property="FontWeight" Value="{Binding Weight}" />
<Setter Property="Foreground " Value="{Binding Foreground}" />
<Setter Property="Margin" Value="0,0,3,0" />
</Style>
</StackPanel.Resources>
<TextBlock DataContext="{Binding PrefixStyle}"/>
<TextBlock DataContext="{Binding ValueStyle}"/>
<TextBlock DataContext="{Binding SuffixStyle}"/>
...
</StackPanel>
或者在 app.xaml 中使用全局命名样式:
<Application>
<Application.Resources>
<Style x:Key="MyTextBlockStyle" TargetType="TextBlock">
<Setter Property="Text" Value="{Binding Text}" />
<Setter Property="FontWeight" Value="{Binding Weight}" />
<Setter Property="Foreground " Value="{Binding Foreground}" />
<Setter Property="Margin" Value="0,0,3,0" />
</Style>
</Application.Resources>
</Application>
其他地方的用法:
<Window>
<StackPanel>
<!-- Single textblock with explicit style -->
<TextBlock DataContext="Blah" Style="{StaticResource MyTextBlockStyle}" />
<!-- Multiple textblocks with implicit style -->
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
<Style TargetType="TextBlock" BasedOn={StaticResource MyTextBlockStyle}" />
</StackPanel.Resources>
<TextBlock DataContext="{Binding PrefixStyle}"/>
<TextBlock DataContext="{Binding ValueStyle}"/>
<TextBlock DataContext="{Binding SuffixStyle}"/>
...
</StackPanel>
</Window>