无法从Xaml中的类调用sub [英] Cannot call sub from class in Xaml

查看:69
本文介绍了无法从Xaml中的类调用sub的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

============================================ ========


MainWindow.xaml


================= ===================================

解决方案

您好OldeEnglishD,


INotifyPropertyChanged接口用于通知视图或ViewModel哪个属性绑定无关紧要;它已更新。


但我没有看到你在View或ViewModel中定义ServerID,也没有在窗口UI中绑定ServerID属性。


例如,我做了一个你可以采取措施的样本:

< StackPanel> 
< StackPanel>
< TextBlock
Width =" 200"
高度="30"
Text =" {Binding Name}" />
< TextBlock
Width =" 200"
高度="30"
Margin =" 10"
Text =" {Binding Age}" />
< / StackPanel>
< Button
Name =" btn1"
Width =" 200"
高度="30"
Click =" btn1_Click"
Content =" btn1" />
< / StackPanel>


 public partial class Window22:Window 
{
public viewmodel1 model {get;组; }
public Window22()
{
InitializeComponent();
model = new viewmodel1();
this.DataContext = model;
}

private void btn1_Click(object sender,RoutedEventArgs e)
{
model.Name =" Cherry" ;;
model.Age = 26;
}
}

公共类viewmodel1:ViewModelBase
{
private string _Name;
公共字符串名称
{
get {return _Name; }
设置
{
_Name = value;
RaisePropertyChanged(" Name");
}
}

private Int32 _Age;
public Int32年龄
{
get {return _Age; }
设置
{
_Age = value;
RaisePropertyChanged(" Age");
}
}

public viewmodel1()
{
Name =" Mattew" ;;
年龄= 24;
}
}


公共类ViewModelBase:INotifyPropertyChanged 
{

公共事件PropertyChangedEventHandler的PropertyChanged;


public void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if(handler!= null)
{
handler(this,new PropertyChangedEventArgs(propertyName));
}
}
}


最好的问候,


Cherry




====================================================

MainWindow.xaml

====================================================

解决方案

Hi OldeEnglishD,

INotifyPropertyChanged interface is used to notify the view or ViewModel that it does not matter which property is binding; it is updated.

But I don't see you define ServerID in View or ViewModel, and you don't bind ServerID property in window UI.

For example, I do one sample that you can take a lo0k:

 <StackPanel>
        <StackPanel>
            <TextBlock
                Width="200"
                Height="30"
                Text="{Binding Name}" />
            <TextBlock
                Width="200"
                Height="30"
                Margin="10"
                Text="{Binding Age}" />
        </StackPanel>
        <Button
            Name="btn1"
            Width="200"
            Height="30"
            Click="btn1_Click"
            Content="btn1" />
    </StackPanel>

public partial class Window22 : Window
    {
        public viewmodel1 model { get; set; }
        public Window22()
        {
            InitializeComponent();
            model = new viewmodel1();
            this.DataContext = model;
        }

        private void btn1_Click(object sender, RoutedEventArgs e)
        {
            model.Name = "Cherry";
            model.Age = 26;
        }
    }

    public class viewmodel1:ViewModelBase
    {
        private string _Name;
        public string Name
        {
            get { return _Name; }
            set
            {
                _Name = value;
                RaisePropertyChanged("Name");
            }
        }

        private Int32 _Age;
        public Int32 Age
        {
            get { return _Age; }
            set
            {
                _Age = value;
                RaisePropertyChanged("Age");
            }
        }

        public viewmodel1()
        {
            Name = "Mattew";
            Age = 24;
        }
    }

public class ViewModelBase:INotifyPropertyChanged
    {
          
        public event PropertyChangedEventHandler PropertyChanged;

        
        public void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

Best Regards,

Cherry


这篇关于无法从Xaml中的类调用sub的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆