Bind a property of a selfmade user control to a property of the main-view-model?

Create another dependency property of type ICommand say 'TextClickCommand'. Create a command in your viewmodel and bind to TextClickCommand and execute this on MouseClick.

Up vote 0 down vote favorite share g+ share fb share tw.

I have a wpf application where I extended the RichTextBox to provide some specific functions. Lets call that new class BetterTextBox. Now, when I click in that TextBox TextBox.

OnPreviewMouseLeftButtonUp is called and I am getting the CaretPosition: protected override void OnPreviewMouseLeftButtonUp(MouseButtonEventArgs e) { PressedOffset = Document.ContentStart. GetOffsetToPosition(CaretPosition); } public static readonly DependencyProperty PressedOffsetProperty = DependencyProperty. Register("PressedOffset", typeof(int), typeof(ByteViewTextBox), new FrameworkPropertyMetadata() { DefaultValue = 0, BindsTwoWayByDefault = true, DefaultUpdateSourceTrigger = UpdateSourceTrigger.

PropertyChanged, }); public int PressedOffset { get { return (int)GetValue(PressedOffsetProperty); } set { SetValue(PressedOffsetProperty, value); Console. WriteLine("klöjgf");} } XAML: MainWindow has MainViewModel as DataContext. What I want to do is, when a MouseClick in BetterTextBox occurs, a function in MainViewModel should be called.

How can I get out of my UserControl a call a function in the MainViewModel? I tried something like this: MainViewModel: public int MyFunction { set { callMyRealFunction(); } } But that doesn't work. There is also a way to register a CallbackFunction for PressedOffsetProperty, but I am not sure how to register there a function from the MainViewModel which is not static.

C# wpf xaml data-binding link|improve this question edited Jun 29 '11 at 14:18H.B.43k61645 asked Jun 29 '11 at 13:12KasF814.

Create another dependency property of type ICommand say 'TextClickCommand'. Create a command in your viewmodel and bind to TextClickCommand and execute this on MouseClick. Edit: You can do it even the other way around create a IsTextSelected bool dependency property in textbox bind to this bool using a propery in view model and call your method in view model property setter.

Sry, I can't really follow you. Can you be more precisly? I am already doing your "Edit" without luck or not?

I also googled for Commands and there is a bunch of results and I am getting lost. – KasF Jun 30 '11 at 7:57.

Just solved by doing from WPF Usercontrol interaction with parent view / viewmodel.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions