Write controls on the VM interface under the wpf mvvm framework, wpfmvvm
Normally, MVVM writes a style on the View page and logic on the ViewModel page, but sometimes writing a style on the View page does not meet the requirement. I recently encountered this project, so I can only write style controls on the VM page, and then bind them to the View page.
First look at the figure
Various taxes need to be changed. Of course, the style is not written on the VM page for this reason, but because the different taxes are red and the corresponding [] is black.
In Textblock, the Run attribute is used to add variables, and the color must be uniform when the Run is added. Therefore, you can only use three Run to set "[", "Tax ", "]", but there is no way to parse the front-end, so you can only use the VM page for writing styles.
Field
private TextBlock textBlock; public TextBlock TextBlock { get { return textBlock; } set { textBlock = value; RaisePropertyChanged("TextBlock"); } }
Textblock style under VM
TextBlock textblock = new TextBlock (); textblock. textWrapping = TextWrapping. wrap; textblock. horizontalAlignment = HorizontalAlignment. center; textblock. margin = new Thickness (20, 0, 20, 0); textblock. fontSize = 30; textblock. horizontalAlignment = HorizontalAlignment. left; textblock. verticalAlignment = verticalignment. top; BrushConverter brushConverter = new BrushConverter (); Brush Blackbrush = (Brush) br UshConverter. convertFromString ("# 2c2c2c"); Brush Redbrush = (Brush) brushConverter. convertFromString ("# a80301"); textblock. foreground = Blackbrush; textblock. inlines. add (new Run () {Text = "", Foreground = Brushes. red}); textblock. inlines. add ("you have this month"); for (int I = 0; I <Mess. count; I ++) {textblock. inlines. add (new Run () {Text = "[", Foreground = Blackbrush}); textblock. inlines. add (new Run {Text = M Ess [I]. toString (), Foreground = Redbrush}); textblock. inlines. add (new Run () {Text = "]", Foreground = Blackbrush});} textblock. inlines. add ("Tax (FEE) type has not been declared, please declare within the levy period. "); TextBlock = textblock;
Write a ScrollViewer on The XAML page to receive it.
<ScrollViewer Content="{Binding TextBlock}" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto"/>