WPF: Binding from

Hello everybody,

Binding is a lot of fun. I still don’t understand it, and I need your help. I have a custom control, just a textbox to display a value of type double. On the long run, I want to change this value with mousedrags, or using the mousewheel, but for now I just want to get a command working when clicking inside the textbox. I’m using MVVMLights RelayCommand.

The ViewModel:

public class MainViewModel : ViewModelBase { public ValueModel ValueModelTool { get; set; } public RelayCommand ChangeValue { get; private set; } public MainViewModel() { ValueModelTool = new ValueModel { Name = "Example", Value = 12.345 }; ChangeValue = new RelayCommand(() => { Trace.TraceInformation("OK"); }); } } 

The View (Just the important parts)

<Window x:Class="VLick.Ui.Desktop.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:VLick.Ui.Desktop" mc:Ignorable="d" DataContext="{Binding Main, Source={StaticResource Locator}}" Title="It will be cool" Height="768" Width="1366" WindowStartupLocation="CenterScreen" Background="#30363f" BorderThickness="20,0,20,5" BorderBrush="#30363f"> <Window.Resources> <ResourceDictionary Source="Resources.Main.xaml"/> </Window.Resources> <ContentControl ContentTemplate="{StaticResource ValueBoxTemplate}" Content="{Binding ValueModelTool}" /> </Window> 

The resource dictionary with the unknown binding

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:VLick.Ui.Desktop"> <DataTemplate x:Key="ValueBoxTemplate"> <GroupBox Header="{Binding Name}" Width="110"> <TextBox Width="100" Height="30" TextAlignment="Center" Text="{Binding Value, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"> <TextBox.InputBindings> <MouseBinding Command="HMMMM????" MouseAction="LeftClick" /> </TextBox.InputBindings> </TextBox> </GroupBox> </DataTemplate> </ResourceDictionary> 

How do I find the bidning for the ChangeValue Command? Do I have to find the binding for the view’s DataContext? I’m sure this is not a big deal.. this is just new to me. Thanks for the help.

submitted by /u/drehwurm
[link] [comments]

Leave a Reply