Wpf 将UI直接绑定到API模型
直接将UI绑定到HTTP API模型而不创建单独的视图模型是一种糟糕的做法吗 唯一的缺点是它不会实现Wpf 将UI直接绑定到API模型,wpf,xaml,mvvm,Wpf,Xaml,Mvvm,直接将UI绑定到HTTP API模型而不创建单独的视图模型是一种糟糕的做法吗 唯一的缺点是它不会实现INotifyPropertyChanged,因此当您从代码中更改属性时,UI不会更新-但是如果您不需要从代码中更新属性该怎么办 这是我的API响应模型: public class EventResponseModel { public int Id { get; set; } public string Subject { get; set; } public strin
INotifyPropertyChanged
,因此当您从代码中更改属性时,UI不会更新-但是如果您不需要从代码中更新属性该怎么办
这是我的API响应模型:
public class EventResponseModel
{
public int Id { get; set; }
public string Subject { get; set; }
public string Description { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public string ArgbColor { get; set; } // should bind to Brush
public Location Location { get; set; }
public IEnumerable<Person> Attendees { get; set; }
}
直接将UI绑定到HTTP API模型而不创建单独的视图模型是一种糟糕的做法吗
如果您现在和将来能够完全按照GUI应用程序中的方式使用模型类,则不会这样做。那么就没有理由仅仅为了它而创建视图模型包装器,因为您基本上依赖于API
创建一个单独的视图模型,映射到它,然后绑定到它,是否总是首选这种方法
不,不总是。例如,如果要在某种网格中显示数百或数千个事件响应,则不希望为每个项调用转换器。在这种情况下,最好包装模型并直接绑定到UI友好的笔刷属性或类似属性。所以这完全取决于你的要求。但是直接绑定到域对象并不总是一个坏主意。“这是一个坏做法吗?”-不,我认为不是。但是,我建议使用
public Color ArgbColor{get;set;}
并绑定SolidColorBrush的颜色属性。Color
需要对System.Media
或PresentationCore
的引用。此模型在服务器上使用,因此string
是最佳类型。ArgbColor
值是否包含html clr代码?像这样(#1A85CD)?@AvinashReddy是的,Aarggbb。使用BrushConverter
转换它不是问题,那么为什么不能将字符串绑定到颜色,我的意思是Background=“{Binding ArgbColor}”
像这样。
public class EventVM
{
public EventResponseModel EditedEvent { get; set; }
// available locations etc., commands for saving
}