使用WPF、MVVM和DP的用户控件[重复]

kmb7vmvb  于 2022-12-14  发布在  其他
关注(0)|答案(3)|浏览(161)

此问题在此处已有答案

XAML binding not working on dependency property?(1个答案)
Issue with DependencyProperty binding(3个答案)
四年前就关门了。
我正在设计一个NumericUpDownControl用户控件,并在我的MainWindow.xaml中成功实现了它,如下所示:

<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>

在UserControl的代码隐藏中,我定义了以下内容:

public static readonly DependencyProperty ValueProperty =
     DependencyProperty.Register(
        "Value", typeof(int),
        typeof(NumericUpDownControl)
     );

  public int Value
  {
     get => (int)GetValue(ValueProperty);
     set => SetValue(ValueProperty, value);
  }

它运行良好,因为我可以在我的MainWindow.xaml中使用它,如下所示:

<controls:NumericUpDownControl Width="100" Value="10"/>

但是,当我尝试为UserControl设置ViewModel时,UserControl中的TextBox不再识别Value依赖项属性。如何正确实现UserControl的ViewModel,同时仍允许从控件外部设置Value属性?当前实现是否存在导致此问题的问题?

brccelvz

brccelvz1#

由于您正在制作一种自定义控件,因此解决问题的另一种方法是使用依赖项属性的propertychanged回调,每次它发生更改时,只需更新Textbox的Text属性即可:

public partial class NumericUpDownControl : UserControl
    {
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(int),typeof(NumericUpDownControl),new PropertyMetadata(OnValueChanged));

        private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((NumericUpDownControl) d).TextBox.Text = e.NewValue?.ToString() ?? string.Empty;
        }

        public int Value
        {
            get => (int)GetValue(ValueProperty);
            set => SetValue(ValueProperty, value);
        }

        public NumericUpDownControl()
        {
            InitializeComponent();
        }
    }
}
vngu2lb8

vngu2lb82#

这会将TextBoxText属性系结至UserControlValue属性,而不论其DataContext为何:

<TextBox Text="{Binding Value, RelativeSource={RelativeSource AncestorType=UserControl}}" Grid.Row="0" Grid.RowSpan="2" Height="20" VerticalContentAlignment="Center"/>
kyvafyod

kyvafyod3#

不能将UserControl同时绑定到自身与ViewModel.
您必须从UserControl中移除DataContext="{Binding RelativeSource={RelativeSource Self}}",并确保在UI中集成UserControl时将正确的DataContext传递给UserControl。

相关问题