WPF从父视图XAML设置UserControl的ViewModel属性[重复]

zdwk9cvp  于 2023-03-16  发布在  其他
关注(0)|答案(2)|浏览(204)

此问题在此处已有答案

Callback when dependency property recieves xaml change(2个答案)
WPF usercontrol Twoway binding Dependency Property(2个答案)
2天前关闭。
我有一个用XAML编写的带有MyControl的MainView。我需要用MVVM编写控件,所以我有MyControl.xaml,它用一些绑定的属性定义布局; MyControl.xaml.cs,它只存储构造函数和DependencyProperties(我需要能够在Styles. xamlResourceDictioanry中设置它们); MainView.xaml(带有主视图UI)-与该MainView关联的C#代码保持不变。
MyControl.xaml:

<UserControl x:Class="MainWindowModule.Views.MyControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
             xmlns:prism="http://prismlibrary.com/" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:viewmodels="clr-namespace:MainWindowModule.ViewModels"
             d:DataContext="{d:DesignInstance Type=viewmodels:MyControlViewModel}"
             prism:ViewModelLocator.AutoWireViewModel="True">

    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <prism:InvokeCommandAction Command="{Binding LoadedCommand}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    
    <Grid>
        <Rectangle x:Name="rect" Width="{Binding Width}" />
    </Grid>
</UserControl>

MyControl.xaml.cs:

public partial class MyControl:UserControl {

        public static readonly DependencyProperty NumberProperty =
            DependencyProperty.Register("Number", typeof(int), typeof(MyControl), new PropertyMetadata(0));

        public MyControl() {
            InitializeComponent();
        }
    }

MyControlViewModel.cs:

public class MyControlViewModel:BindableBase {

        public int Number {
            get { return _number; }
            set { SetProperty(ref _number, value); }
        }

        public int Width {
            get { return _width; }
            set { SetProperty(ref _width, value);}
        }

        public DelegateCommand LoadedCommand { get; set; }

        private int _number = 0;
        private int _width = 100;

        public MyControlViewModel() {
            LoadedCommand = new DelegateCommand(OnLoad);
        }

        private void OnLoad() {
            //Load resources
        }
    }

MyControl的MainView.xaml声明:

<Grid>
   <local:MyControl x:Name="test" />
</Grid>

我的问题是,我希望能够在MainView.xaml中指定Number属性值,但我看不到此属性。

u5rb5r59

u5rb5r591#

由于MyControlViewModel的示例存储在DataContext属性中,因此其属性应该能够通过DataContext.[property name]访问。
因此,您可以将Number属性与MainView.xaml中的其他控件绑定,如下所示。

<ListView SelectedIndex="{Binding ElementName=test, Path=DataContext.Number, Mode=OneWayToSource}"/>
wgxvkvu9

wgxvkvu92#

在MyControl.xaml.cs中,还需要dependency属性的支持属性

public static readonly DependencyProperty NumberProperty =
        DependencyProperty.Register("Number", typeof(int), typeof(MyControl), new PropertyMetadata(0));

public int Number
{
    get => (int)GetValue(NumberProperty );
    set => SetValue(NumberProperty , value);
}

相关问题