开发者

How to Refresh ReadOnly property bound to a DataGrid

开发者 https://www.devze.com 2022-12-18 09:34 出处:网络
A have an Order class with a ReadOnly TotalPrice property that calcutaltes from other properties of the class. Throught an ObservableCollection I make a binding to a DataGrid. The code is below.

A have an Order class with a ReadOnly TotalPrice property that calcutaltes from other properties of the class. Throught an ObservableCollection I make a binding to a DataGrid. The code is below.

Order Class

public class Order
{
    public String Name { get; set; }
    public Double Price { get; set; }
    public Int32 Quantity { get; set; }
    public Double TotalPrice { get { return Price * Quantity; } }
}

XAML Code for DataGrid

<!--DataGrid-->
<my:DataGrid AutoGenerateColumns="False" Name="dgOrders" ItemsSource="{Binding}">
    <my:DataGrid.Columns>
        <my:DataGridTextColumn Binding="{Binding Name}" Header="Name" IsReadOnly="True" />
        <my:DataGridTextColumn Binding="{Binding Price}" Header="Price" IsReadOnly="True" />
        <my:DataGridTextColumn Binding="{Binding Quantity}" Header="Quantity"  />
        <my:DataGridTextColumn Binding="{Binding Total, Mode=OneWay}" Header="Total" IsReadOnly="True" />
    </my:DataGrid.Columns>
</my:DataGrid>

Bind the Class to the DataGrid

ObservableCollection<Order> Orders = new ObservableCollection<Order>();

Orders.Add(new Order() { Name = "Book", Quantity = 1, Price = 13 });
Orders.Add(new Order() { Name = "Pencil", Quantity = 2, Price = 4 })开发者_Go百科;
Orders.Add(new Order() { Name = "Pen", Quantity = 1, Price = 2 });

dgOrders.DataContext = Orders;

Now, when user updates Quantity column on the DataGrid TotalPrice column should be updates as the TotalPrice property updates itself. My consumption is that since TotalPrice is not updated it does not produce a notify as other properties do so datagrid won't update.

Thanks for any comments.

Edit: Let me clarify my question.

I need a sort of notification system that tells the UI to update itself when a readonly property changes because of an inner change.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号