尝试使用接口inotifychange并使用类和静态类绑定组合框 [英] Trying interface inotifychange and binding combobox with class and static class

查看:92
本文介绍了尝试使用接口inotifychange并使用类和静态类绑定组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我通过评论和看微软试着做你说的话。我在属性中使用INotifyChange创建了一个具有3个属性(Name,缩写和List< state>)的类State。然后我创建了一个带有List of state的静态类,并在构造函数中将所有50个状态添加到List中,并创建了一个方法将列表返回到一个数组。我不知道还需要做什么才能将我的列表绑定到组合框我觉得我非常接近。

Hello I tried doing what you said by doing comment and looking at Microsoft. I made a class State with 3 properties (Name, abbreviations, and List<state> using INotifyChange in the properties. Then I made a static class with a List of state and in the constructor adding all 50 states to the List and a method to return the list to an array. Im not sure what else I need to do to bind my list to the combobox I feel that I am very close.

public class US_State : INotifyPropertyChanged
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                NotifyPropertyChanged();
            }
        }
        private string _abbreviations;
        public string Abbreviations
        {
            get => _abbreviations;
            set
            {
                _abbreviations = value;
                NotifyPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            //if(PropertyChanged!=null)PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        private List<US_State> _allusstates;
        public List<US_State> AllUSStates
        {
            get => _allusstates;
            set
            {
                _allusstates = value;
                NotifyPropertyChanged();
            }
        }

        public US_State()
        {
            Name = null;
            Abbreviations = null;
        }
        public US_State(string ab, string name)
        {
            Name = name;
            Abbreviations = ab;
        }
        public override string ToString()
        {
            return String.Format("{0} - {1}", Abbreviations, Name);
        }
    }
    //Static class of all 50 US_state's
    public static class StatesArray
    {
        public static List<US_State> states;
        
        static StatesArray()
        {
            states = new List<US_State>(50);
            states.Add(new US_State("AL", "Alabama"));
            states.Add(new US_State("AK", "Alaska"));
//and the rest of the 50 states added just like this way above.        }
        public static US_State[] AllFiftyStates()
        {
            return states.ToArray();
        }
    }
Code in WPF Window
    public partial class ProfileScreen : Window
    {
private List<US_State> Get_States = StatesArray.states;
xaml code
<ComboBox x:Name="ComboboxState" Grid.Column="1" Grid.Row="2" VerticalAlignment="Center" Width="120" HorizontalAlignment="Left" ToolTip="Enter your state."
                          ItemsSource="{Binding Get_States, RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
                          DisplayMemberPath="Name" SelectedValuePath="Abbreviation"/>





我的尝试:



数据表加载但想要使用此接口方式而不是



What I have tried:

data table loading but wanting to use this interface way instead

推荐答案

您好b $ b

1.你的Us_State类很好。发现没问题。为什么在Us_State类中创建AllUSStates公共属性。它不是必需的。

2.你的静态类StatesArray很好,但不需要AllFiftyStates方法,你可以使用public getter和private setter将state声明为公共属性。所以在构造函数上只填充状态属性,而公开则可以访问它。您还可以通过转换List< us_states>来增加额外费用。到us_states的数组。

注意:这是建议。



3.现在终于完成后,在mainWindow.xaml.cs中创建公共属性。并实现INoityPropertyChanged。

Hi
1. Your Us_State class is fine. Found No issue. Why you created AllUSStates public property in Us_State class. its not required.
2. Your static class StatesArray is Fine, but AllFiftyStates method is not required, You can declare states as public property, with public getter and private setter. So on constructor just fill states property, while publicly you can access it. Also you are adding additional cost by converting List<us_states> to array of us_states.
Note: this is suggestion.

3. Now finally once you have done, create public property in your mainWindow.xaml.cs. and implement INoityPropertyChanged.
private List<US_State> get_States;
public List<US_States> Get_States{
get=>get_States;
set{
      get_States = value;
      RaisePropertyChange();
   }
}





4.现在在窗口加载事件中从Get_States = StatesArray.states填充来自源的Get_States ;



5.最后找到Combobox的Get_States。但请记住,Combobox会查找它自己的datacontext,然后搜索throgh到它的祖先。但是这里因为你的Get_States可以在Windows.xaml.cs中使用,所以你将使用相对来源,例如





4. Now On window load event fill Get_States from source like Get_States= StatesArray.states;

5. Finally Find Get_States to Combobox. but remember, Combobox will look for datacontext of itesself, and then search throgh to its ancestor. But here as Your Get_States are availble in Windows.xaml.cs so you will use relative source like

<ComboBox x:Name="ComboboxState" Grid.Column="1" Grid.Row="2" VerticalAlignment="Center" Width="120" HorizontalAlignment="Left" ToolTip="Enter your state."

                          ItemsSource="{Binding Get_States, RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"

                          DisplayMemberPath="Name" SelectedValuePath="Abbreviation"/>


这篇关于尝试使用接口inotifychange并使用类和静态类绑定组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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