Write controls on the VM interface under the wpf mvvm framework, wpfmvvm

Source: Internet
Author: User

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"/>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.