C# Can';沙马林中SwitchCell的t集结合
我正在尝试创建一个包含元素列表的SwitchCell。 尽管我通过stackoverflow找到了使用普通字符串列表的方法,但当我尝试将单元格属性绑定到自制的结构时,我无法发现我做错了什么 这是我的代码:C# Can';沙马林中SwitchCell的t集结合,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我正在尝试创建一个包含元素列表的SwitchCell。 尽管我通过stackoverflow找到了使用普通字符串列表的方法,但当我尝试将单元格属性绑定到自制的结构时,我无法发现我做错了什么 这是我的代码: public class RestaurantFilter { public List<FilterElement> Types; public RestaurantFilter(List<string> types) { Ty
public class RestaurantFilter
{
public List<FilterElement> Types;
public RestaurantFilter(List<string> types)
{
Types = new List<FilterElement>();
foreach (string type in types)
Types.Add(new FilterElement { Name = type, Enabled = false });
}
}
public struct FilterElement
{
public string Name;
public bool Enabled;
}
public FilterPage()
{
List<string> l = new List<string>(new string[] { "greek", "italian", "bavarian" });
RestaurantFilter filter = new RestaurantFilter(l);
ListView types = new ListView();
types.ItemTemplate = new DataTemplate(() =>
{
var cell = new SwitchCell();
cell.SetBinding(SwitchCell.TextProperty, "Name");
cell.SetBinding(SwitchCell.IsEnabledProperty, "Enabled");
return cell;
});
types.ItemsSource = filter.Types;
Content = types;
}
公共类RestaurantFilter
{
公共列表类型;
公共餐厅过滤器(列表类型)
{
类型=新列表();
foreach(类型中的字符串类型)
Add(新的FilterElement{Name=type,Enabled=false});
}
}
公共结构过滤器
{
公共字符串名称;
公共布尔启用;
}
公共过滤器页()
{
列表l=新列表(新字符串[]{“希腊语”、“意大利语”、“巴伐利亚语”});
RestaurantFilter=新的RestaurantFilter(l);
ListView类型=新建ListView();
types.ItemTemplate=新数据模板(()=>
{
var cell=新的SwitchCell();
cell.SetBinding(SwitchCell.TextProperty,“Name”);
cell.SetBinding(SwitchCell.IsEnabledProperty,“已启用”);
返回单元;
});
types.ItemsSource=filter.types;
内容=类型;
}
但是应用程序中的SwitchCell没有显示名称或布尔值。关于IsEnabled属性-似乎存在一个Knoww错误,IsEnabled属性将在Xamarin中修复。Forms 2.3.0-pre1版本可能与您的案例有关: 关于Name属性-尝试将FilterElement结构更改为具有properties和PropertyChangedEventHandler的类,如下所示:
public class FilterElement
{
public event PropertyChangedEventHandler PropertyChanged;
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
}
private bool _enabled;
public bool Enabled
{
get { return _enabled; }
set
{
_enabled = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Enabled"));
}
}
}
}
这样,您将能够更新类型列表,并且它将自动更新ListView
顺便说一下,如果要根据ViewModels打开或关闭过滤器(而不是启用或禁用它),则需要使用OnProperty进行绑定:
感谢您指出我使用了错误的属性!:)