I am trying to design a UserControl specifically a NumericUpDownControl.
<UserControl x:Class="SettingsDialog.Controls.NumericUpDownControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:SettingsDialog.Controls"
xmlns:viewModels="clr-namespace:SettingsDialog.ViewModels"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="18"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBox Text="{Binding Value}" Grid.Row="0" Grid.RowSpan="2" Height="20" VerticalContentAlignment="Center"/>
<RepeatButton Content="5" Grid.Column="1" Grid.Row="0" FontSize="8" Height="10" FontFamily="Marlett" VerticalContentAlignment="Center"/>
<RepeatButton Content="6" Grid.Column="1" Grid.Row="1" FontSize="8" Height="10" FontFamily="Marlett" VerticalContentAlignment="Center"/>
</Grid>
</UserControl>
In the code-behind of UserControl I have the following:
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register(
"Value", typeof(int),
typeof(NumericUpDownControl)
);
public int Value
{
get => (int)GetValue(ValueProperty);
set => SetValue(ValueProperty, value);
}
It works fine as I can use it in my MainWindow.xaml like this:
<controls:NumericUpDownControl Width="100" Value="10"/>
But the problem is I also want a ViewModel for my UserControl. And when I set it then the TextBox in my UserControl does not recognize the Value dependency property anymore. How can I have a ViewModel and at the same time set the Value from outside the UserControl? Am I doing something seriously wrong?