[英]WPF TextBlock with custom StringFormat and variable number of Run/Inline
我有一个需要显示的短语列表。 这些短语具有静态文本和可变数量的位,需要在屏幕上突出显示(粗体,颜色不同)。
如果我要从xaml构建所有内容,这是一个示例。
<TextBlock Grid.Row="2">
<Run Text="Your"/>
<Run Text="{Binding Text1}"/>
<Run Text="has a limit of $"/>
<Run Text="{Binding Limit}" FontWeight="Bold" Foreground="Red"/>
<Run Text="for period "/>
<Run Text="{Binding StartDate}" FontWeight="Bold" Foreground="Blue"/>
<Run Text=" : "/>
<Run Text="{Binding EndDate}" FontWeight="Bold" Foreground="Blue"/>
<Run Text=". Your up to date expenses are $"/>
<Run Text="{Binding Expenses}" FontWeight="Bold" Foreground="Red"/>
</TextBlock>
<TextBlock Grid.Row="3">
<Run Text="Your"/>
<Run Text="{Binding Text2}"/>
<Run Text="has a limit of $"/>
<Run Text="{Binding Limit2}" FontWeight="Bold" Foreground="Red"/>
<Run Text="for period "/>
</TextBlock>
我想要实现的是以某种方式将文本与{0}绑定在一起,并绑定ItemsSource
,它将为TextBlock
设置Inlines
,并且进一步为每个内联样式(字体粗细和文本颜色)绑定ItemsSource
应该有:
文字:您的{0}的期间上限为$ {1}。
填充内联的值:abc,1000
适用的样式{none,none},{bold,red}
首先有可能吗? 如果是这样,您能为我指出正确的方向还是给我一些提示?
谢谢。
我将为此创建一个自定义TextBlock
后代,并以编程方式创建内联。
首先,创建一个模型来控制文本的显示:
public class TextModel
{
public string Text { get; set; }
public Color Color { get; set; }
public FontWeight Weight { get; set; }
}
您可能要对此实现INotifyPropertyChanged
,尽管稍后会使事情复杂化。
接下来,您需要以下这些的集合:
public class TextModelCollection : ObservableCollection<TextModel>
{
}
最后,创建一个派生自TextBlock
的自定义类:
public class MyCustomTextBlock : TextBlock
{
public static readonly DependencyProperty ItemsProperty =
DependencyProperty.Register("Items", typeof(TextModelCollection),
typeof(MyCustomTextBlock), new PropertyMetadata(OnItemsChanged));
public TextModelCollection Items
{
get
{
return (TextModelCollection) GetValue(ItemsProperty);
}
set
{
SetValue(ItemsProperty, value);
}
}
public MyCustomTextBlock()
{
RefreshInlines();
}
static void OnItemsChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
((MyCustomTextBlock) d).RefreshInlines();
}
}
然后,您只需要一个方法(在此称为RefreshInlines
),即可在自定义属性更改时刷新文本块的inlines集合。
void RefreshInlines()
{
Inlines.Clear();
foreach (TextModel text in Items)
{
var run = new Run(text.Text);
run.Foreground = new SolidColorBrush(text.Color);
run.FontWeight = text.Weight;
Inlines.Add(run);
}
}
由于TextModelCollection
是可观察的集合,因此您需要注册列表更改,以便在添加或删除项目时刷新内联。 我没有在这里显示该代码,但它基本上与OnItemsChanged
方法相关。
这实际上是两个问题:
您正在寻找MultiBinding
和StringFormat
:
这是针对文本以静态文本开头的情况:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="Write Static Text Here {1} Something else {1}">
<Binding Path="Text2" />
<Binding Path="Text2" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
这是针对Text以可变文本开头的情况:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} Write Static Text Here {1} Something else">
<Binding Path="Text2" />
<Binding Path="Text2" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
您需要使用诸如ItemsControl
东西( ListBox
DataGrid
等)
<ItemsControl ItemsSource="{Binding MyList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
put anything here to display as an element of MyList
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
您还需要在窗口的DataContext
中为MyList
创建一个集合。 例如:
public MainWindow()
{
InitializeComponent();
var vm = new MainVm();
DataContext = vm;
}
这个MainVm
应该从DependencyObject
派生,并包含类似以下内容:
public ObservableCollection<string> MyList { get { return _myList; } }
private ObservableCollection<string> _myList = new ObservableCollection<string>();
有关更多信息,请访问google MVVM。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.