Return to site

Wpf Textblock Multiline

broken image


Multiline

Where is the reset button on a dirt devil vacuum. One thing about WPF that's very nice is that all of the controls are very lookless. Because of this, we can make use of TextBox, which has a LineCount property (Why it's not a DependencyProperty or why TextBlock doesn't also have it I do not know). With the TextBox, we can simply re-template it so it behaves and looks more like a TextBlock. In our custom Style/Template we're going to set IsEnabled to False, and just create a basic re-templating of the control so that the disabled look is no longer present. We can also bind any properties we want to maintain, like Background, through the use of TemplateBindings.

Xaml Textbox Multiline

Now, that will take care of making our TextBox look and behave like a TextBlock, but how do we get the line count?

Wpf Textblock Multiline Center

Well, if we want to access it directly in the code behind then we can register to the TextBox's SizeChanged Event.

Wpf Textblock Multi Lines

This example shows how to use Extensible Application Markup Language (XAML) to define a TextBox control that will automatically expand to accommodate multiple lines of text. Example Setting the TextWrapping attribute to Wrap will cause entered text to wrap to a new line when the edge of the TextBox control is reached, automatically expanding. Here is another request that a friend of mine asked me recently. To achieve this, I've just create a custom control that contains a TextBlock and a TextBox (which is hidden) controls. When user double clic on the TextBlock, the Textbox appears. So here is my proposition: in the file generic.xaml, put this. I have a TextBox in WPF for information, and where someone can take notes. The user can write in it, save it, etc. But, for some reason the TextBox won't take the enter key while the app is running. If you type enough characters to get to the end of the text box a new line is made, but it won't make one if you press enter.

Wpf Textblock Multiline

However, since I tend to need to use properties like that in places other then the current window, and/or am using MVVM and don't want to take that approach, then we can create some AttachedProperties to handle the retrieval and setting of the LineCount. We're going to use the AttachedProperties to do the same thing, but now we'll be able to use it with any TextBox anywhere, and bind to it through that TextBox instead of the Window's DataContext.

Wpf Textblock Multiline

Where is the reset button on a dirt devil vacuum. One thing about WPF that's very nice is that all of the controls are very lookless. Because of this, we can make use of TextBox, which has a LineCount property (Why it's not a DependencyProperty or why TextBlock doesn't also have it I do not know). With the TextBox, we can simply re-template it so it behaves and looks more like a TextBlock. In our custom Style/Template we're going to set IsEnabled to False, and just create a basic re-templating of the control so that the disabled look is no longer present. We can also bind any properties we want to maintain, like Background, through the use of TemplateBindings.

Xaml Textbox Multiline

Now, that will take care of making our TextBox look and behave like a TextBlock, but how do we get the line count?

Wpf Textblock Multiline Center

Well, if we want to access it directly in the code behind then we can register to the TextBox's SizeChanged Event.

Wpf Textblock Multi Lines

This example shows how to use Extensible Application Markup Language (XAML) to define a TextBox control that will automatically expand to accommodate multiple lines of text. Example Setting the TextWrapping attribute to Wrap will cause entered text to wrap to a new line when the edge of the TextBox control is reached, automatically expanding. Here is another request that a friend of mine asked me recently. To achieve this, I've just create a custom control that contains a TextBlock and a TextBox (which is hidden) controls. When user double clic on the TextBlock, the Textbox appears. So here is my proposition: in the file generic.xaml, put this. I have a TextBox in WPF for information, and where someone can take notes. The user can write in it, save it, etc. But, for some reason the TextBox won't take the enter key while the app is running. If you type enough characters to get to the end of the text box a new line is made, but it won't make one if you press enter.

However, since I tend to need to use properties like that in places other then the current window, and/or am using MVVM and don't want to take that approach, then we can create some AttachedProperties to handle the retrieval and setting of the LineCount. We're going to use the AttachedProperties to do the same thing, but now we'll be able to use it with any TextBox anywhere, and bind to it through that TextBox instead of the Window's DataContext.

Wpf Textblock Multiline Scrollbar

Now, it's simple to find the LineCount:





broken image