Wpf 基于枚举的绑定属性
我的WCF应用程序支持从远程服务下载书籍。 用户向服务发送一个下载书籍的请求,服务获得该请求并不加载书籍。作为响应,它根据enum值向客户端发送下载进度 我的看法:Wpf 基于枚举的绑定属性,wpf,data-binding,Wpf,Data Binding,我的WCF应用程序支持从远程服务下载书籍。 用户向服务发送一个下载书籍的请求,服务获得该请求并不加载书籍。作为响应,它根据enum值向客户端发送下载进度 我的看法: 状态为枚举值 public enum Status { Pending, Started, Completed }; 图书模型 public class BookModel { public string Title { get; set; } public string Author {
状态为枚举值
public enum Status { Pending, Started, Completed };
图书模型
public class BookModel
{
public string Title { get; set; }
public string Author { get; set; }
public string Description { get; set; }
public Status Status { get; set; }
}
我的任务是根据枚举值更新ui
挂起-应显示0%已填充饼图
说明-应显示50%的填充饼图
已完成-应显示100%填充的饼图
需要更新的属性是PieSlice对象(在Book.xaml内部)的“EndAngle”(双数据类型)。
我将此属性绑定到BookViewModel中的“百分比”对象,但是当书籍状态发生更改时,饼图不会使用新的状态值进行更新
谢谢
Book.xaml
<Label Grid.Row="0">Title</Label>
<Label Grid.Row="1">Author</Label>
<Label Grid.Row="2">Description</Label>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Title}"/>
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Author}"/>
<TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Description}"/>
<Button Grid.Column="2" Grid.RowSpan="3" Command="{Binding SaveCommand}" Content="Download" />
<Ellipse Grid.Column="3"
Height="20" Width="20"
Stroke="Black"
StrokeThickness="0.5"
HorizontalAlignment="Center"
Grid.Row="1"
/>
<Controls:PieSlice Grid.Column="3" Grid.Row="1" Stroke="Black" Fill="Black"
Height="20" Width="20"
StartAngle="0" EndAngle="{Binding Percent}"
HorizontalAlignment="Center" />
标题
作者
描述
BookViewModel
public class BookViewModel : ViewModelBase
{
private readonly BookModel Book;
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public BookViewModel(BookModel model)
{
this.Book = model;
}
public double Percent
{
get
{
return Book.Status == Status.Pending ? 0 :
Book.Status == Status.Started ? 180 :
Book.Status == Status.Completed ? 360 : 0;
}
}
public Status Status
{
get
{
return Book.Status;
}
set
{
Book.Status = value;
RaisePropertyChanged("Status");
}
}
}
公共类BookViewModel:ViewModelBase
{
私人只读图书模型书;
///
///初始化MainViewModel类的新实例。
///
公共BookViewModel(BookModel模型)
{
这本书=模型;
}
公共双百分比
{
得到
{
还书。状态==状态。挂起?0:
Book.Status==Status.Started?180:
Book.Status==Status.Completed?360:0;
}
}
公众地位
{
得到
{
还书状态;
}
设置
{
账面状态=价值;
RaisePropertyChanged(“状态”);
}
}
}
您没有为百分比
财产筹集任何财产变更。
您可以将RaisePropertyChanged(“百分比”)代码>紧接着RaisePropertyChanged(“状态”)代码>因此,当您更改状态
值百分比
时,通知也会被提升
通过在Status的setter中调用RaisePropertyChanged(“百分比”)
视图将查询百分比,从而检索正确的值
视图被限定为百分比,这是一个计算属性。更新状态时,百分比(属性更新)但绑定到百分比的视图将不会更新。这是因为视图不知道百分比发生了变化(我们从未告诉过它)。百分比也没有支持属性
public Status Status
{
get
{
return Book.Status;
}
set
{
Book.Status = value;
RaisePropertyChanged("Status");
RaisePropertyChanged("Percent");
}
}