I am new to MVVM WPF. Can anyone tell me if I can use some textbox functions from View in my ViewModel, like textBox.GetLastVisibleLineIndex(), and if I can, how?
Thanks.
EDIT
Ok, I will provide full explanation of my question. I am making some kind of text editor like notepad, for large files over GB, and I am using WPF MVVM approach for that. In my algorithm for reading that large file I have this: I read some portion of file at first, and after that I need to read other parts, and for that I'm using one background thread to check if user scrolled above middle of textbox and if it's that case then thread reads more from file and updates textbox. Now, for that algorithm I need to see what is the last visible line in my textbox, and for that I need to know index of that line with textBox.GetLastVisibleLineIndex(), but from my ViewModel I don't have access to that method. So, basically I need to know how to get last visible line from textBox to my ViewModel.
From your one line of code it is impossible to tell any context for your question.
I guess this is why it's being voted down.
This is therefore just to give you the flavour.
Add an attached dp to your view:
public partial class MainWindow : Window
{
public static DependencyProperty LineNoProperty =
DependencyProperty.RegisterAttached("LineNo"
, typeof(int)
, typeof(MainWindow)
, new FrameworkPropertyMetadata(null)
{ BindsTwoWayByDefault = true });
public static int GetDoc(DependencyObject obj)
{
return (int)obj.GetValue(LineNoProperty);
}
public static void SetDoc(DependencyObject obj, int value)
{
obj.SetValue(LineNoProperty, value);
}
Bind that to a property of your viewmodel in the window tag. You will need to compile first or this'll get blue squiggles.
local:MainWindow.LineNo="{Binding someVMProperty}"
Somewhere, due to some interaction that you have not explained you set that to the line number:
this.SetCurrentValue(LineNoProperty, newValue);
The value will then transfer via binding to your property in the viewmodel and you can do whatever you want to do with it there.
Bear in mind that mvvm doesn't always mean you have no code at all in the view. The view does things are "view responsibilities" and it's only the view which knows about the textbox, let alone which line is the last one in it.
If scrolling is what drives this then I would add either an event handler directly to the view or encapsulate that in a behavior if this needs to be re-usable.
The scrolling in a textbox happens because it has a scrollviewer inside it.
You can handle the bubbling events from that at the textbox level.
https://msdn.microsoft.com/en-us/library/system.windows.controls.scrollviewer.scrollchanged.aspx
That would then presumably be where you'd put your code to find the last line and set your dependency property.
If the viewmodel then needs to do something as the line number changes, you would call a method from the setter of your viewmodel property.