将UserControl属性绑定到ViewModel

时间:2020-08-26 07:41:58

标签: c# wpf mvvm

我有一个国家代码组合框和一个数字文本框的UserContol组合。

在我的窗口中,我有2个UserControl(主电话号码和辅助电话号码)

这是我的模特

     public class ContactNumber
    {
        public Country Country { get; set; }
        public string Number { get; set; }
    }

    public class Country
    {
        public int id { get; set; }
        public string name { get; set; }
        public string code { get; set; }
        public string phone_code { get; set; }
        public int is_active { get; set; }
        public List<Country> country { get; set; }
    }

这是我的UserControl XAML

 <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="90"></ColumnDefinition>
                <ColumnDefinition Width="10"></ColumnDefinition>
                <ColumnDefinition Width="250"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <!--ComboBox for Country-->
            <ComboBox Grid.Column="0" Style="{StaticResource ComboBoxMerged}" x:Name="cmbCountry"></ComboBox>
            <TextBlock Text="|" FontSize="{StaticResource fontSize30}" Foreground="{StaticResource LightSilverBrush}" 
                       HorizontalAlignment="Center" VerticalAlignment="Top" Grid.Column="1" Margin="0,-5,0,0">
            </TextBlock>
            <!--TextBox for Number-->
            <TextBox Style="{StaticResource TextBoxWithDropDown}" 
                     Text="{Binding ContactNumber.Number,ElementName=ContactWindow,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                     x:Name="txtContactNumber" TextChanged="txtContactNumber_TextChanged" Width="240" Grid.Column="2">
            </TextBox>
        </Grid>

这是我后面的控制代码

   public ContactNumber ContactNumber
        {
            get { return (ContactNumber)GetValue(ContactNumberProperty); }
            set 
            { 
                SetValue(ContactNumberProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for ContactNumber.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ContactNumberProperty =
            DependencyProperty.Register("ContactNumber", typeof(ContactNumber), typeof(ContactNumberWithCountryCode),
                new FrameworkPropertyMetadata(
                null,
                FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                SetText));

        private static void SetText(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ContactNumberWithCountryCode contactNumberWithCountryCode=d as ContactNumberWithCountryCode;
            if (contactNumberWithCountryCode!=null)
            {
                contactNumberWithCountryCode.txtContactNumber.Text = (e.NewValue as ContactNumber).Number.ToString();
            }
        }

        public ContactNumberWithCountryCode()
        {
            InitializeComponent();
        }

        private void txtContactNumber_TextChanged(object sender, TextChangedEventArgs e)
        {
            ContactNumber.Number = txtContactNumber.Text;

        }

这是我的主窗口XAMl

                <usercontrols:ContactNumberWithCountryCode ContactNumber="{Binding PrimaryContactNumber,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" x:Name="PracticePhoneNumber"  Margin="0,15,0,0" ></usercontrols:ContactNumberWithCountryCode>
                <usercontrols:ContactNumberWithCountryCode ContactNumber="{Binding SecondryContactNumber,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" x:Name="SecondaryPracticePhoneNumber"  Margin="0,15,0,0" ></usercontrols:ContactNumberWithCountryCode>

这是我的查看模型代码

   private ContactNumber primaryContactNumber;

        public ContactNumber PrimaryContactNumber
        {
            get { return primaryContactNumber; }
            set 
            { 
                primaryContactNumber = value;
                OnPropertyChanged("PrimaryContactNumber");
            }
        }

        private ContactNumber secondryContactNumber;

        public ContactNumber SecondryContactNumber
        {
            get { return secondryContactNumber; }
            set
            {
                secondryContactNumber = value;
                OnPropertyChanged("SecondryContactNumber");
            }
        }

我的目标是在文本或组合框值更改上填充ViewModel属性。

预先感谢。

0 个答案:

没有答案
相关问题