listview:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Models.kuContext ku = new Models.kuContext();
BindingList<Models.Warehouse> r =new BindingList<Models.Warehouse>( ku.Warehouses.Select(m => m).ToList());
((GridView)_list.View).Columns[0].DisplayMemberBinding = new Binding { Path = new PropertyPath(nameof(Models.Warehouse.Name)) };
((GridView)_list.View).Columns[1].DisplayMemberBinding = new Binding { Path = new PropertyPath(nameof(Models.Warehouse.Classify)), Converter = new Classify() };
((GridView)_list.View).Columns[2].DisplayMemberBinding = new Binding { Path = new PropertyPath(nameof(Models.Warehouse.State)), Converter = new Status() };
((GridView)_list.View).Columns[3].DisplayMemberBinding = new Binding { Path = new PropertyPath(nameof(Models.Warehouse.Region)), Converter = new Region() };
((GridView)_list.View).Columns[4].DisplayMemberBinding = new Binding { Path = new PropertyPath(nameof(Models.Warehouse.Direction)) };
((GridView)_list.View).Columns[5].DisplayMemberBinding = new Binding { Path = new PropertyPath(nameof(Models.Warehouse.Remark)) };
((GridView)_list.View).Columns[6].DisplayMemberBinding = new Binding { Path = new PropertyPath(nameof(Models.Warehouse.LengthScale)) };
((GridView)_list.View).Columns[7].DisplayMemberBinding = new Binding { Path = new PropertyPath(nameof(Models.Warehouse.WidthScale)) };
((GridView)_list.View).Columns[8].DisplayMemberBinding = new Binding { Path = new PropertyPath(nameof(Models.Warehouse.PositionX)) };
((GridView)_list.View).Columns[9].DisplayMemberBinding = new Binding { Path = new PropertyPath(nameof(Models.Warehouse.PositionY)) };
_list.ItemsSource = r;
}
private class Region : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Models.kuContext k = new Models.kuContext();
int id = (int)value;
var c = k.Regions.FirstOrDefault(m => m.Id == id);
if (c != null)
{
return c.Name;
}
return "";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
textbox:
t1 t = new t1 { a="1",b="2"};
private void Window_Loaded(object sender, RoutedEventArgs e)
{
tb.SetBinding(TextBox.TextProperty, new Binding { Path = new PropertyPath("a") });//lable l1.SetBinding(Label.ContentProperty, ("FilePath"));
tb.DataContext = t;
}
class t1: INotifyPropertyChanged
{
private string _a, _b;
public string a { get { return _a; } set { _a = value; OnPropertyChanged(new PropertyChangedEventArgs("a")); } }
public string b { get { return _b; } set { _b = value; OnPropertyChanged(new PropertyChangedEventArgs("b")); } }
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
t.a = "22";
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var aa = t;
}
//第二种写法
class Bean : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged; //属性改变时触发的事件
private string filePath; //文件路径名
public string FilePath //实际中用到的变量名
{
get
{
return this.filePath;
}
set
{
if (this.filePath != value) //如果当前的变量值不等于先前的文件名,说明需要更新文件名
{
this.filePath = value; //更新文件名
if (PropertyChanged != null) //如果已经触发了改变事件
{
//通知绑定此变量的textbox在前台更新
PropertyChanged(this, new PropertyChangedEventArgs("FilePath"));
}
}
}
}
}
combobox/listbox:
BindingList<Models.NameValue<string>> csex = new BindingList<Models.NameValue<string>>();
foreach (var item in Enum.GetNames(typeof(Models.CustomerSex)))
{
csex.Add(new Models.NameValue<string> { Name = item, Value = item });
}
s1_c_sex.ItemsSource = csex;
s1_c_sex.DisplayMemberPath = "Name";
s1_c_sex.SelectedValuePath = "Value";