[英]How do I create a reusable TextBlock binding in WPF?
我一直在自學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
}
如何在XAML中只定義一次TextBlock
到TextBlockStyle
的綁定,而不必像上面那樣顯式綁定每個TextBlock
屬性呢? 所以我每個TextBlock
只能一行:
<StackPanel Orientation="Horizontal">
<TextBlock Source="{Binding PrefixStyle}" />
<TextBlock Source="{Binding ValueStyle}" />
<TextBlock Source="{Binding SuffixStyle}" />
...
</StackPanel>
我只是不知道從哪里開始。 我是否需要繼承TextBlock? 使用BindingGroup?
這肯定是程序員遇到的常見問題-因此,如果您以前曾問過這個問題,我深表歉意。 我已經嘗試過搜索,但是我對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 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 { }
您將全部由哪Children.Add(new FolderVM(folder));
來做Children.Add(new FolderVM(folder));
在DriveVM
內部,這將在驅動器下顯示一個文件夾。 問題是這可能變得非常復雜。 簡而言之,我認為繼承是使用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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.