我想做的不仅是将属性 X 绑定到滑块,还将涉及 X 的计算结果绑定到饼图,因此当用户滑动滑块时,饼图会自动更新。
以下是我现在所拥有的。当我滑动“食物”滑块时,饼图会相应更新。但是,如果移动“租用”滑块,则不会发生任何事情。我猜renterInsurance.Value = rent.Value * 0.1
只评估一次,因此在执行后期的任何更改rent.Value 都不会影响renterInsurance.Value
。我如何实现我所需要的?实际计算可能涉及其他属性/变量。
public partial class MainPage : UserControl
{
public class Expense : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _Name;
public string Name
{
get { return _Name; }
set
{
_Name = value;
NotifyPropertyChanged("Name");
}
}
private double _Value;
public double Value
{
get { return _Value; }
set
{
_Value = value;
NotifyPropertyChanged("Value");
}
}
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public MainPage()
{
InitializeComponent();
PieSeries ps = exampleChart.Series[0] as PieSeries;
List<Expense> expenses = new List<Expense>();
Expense food = new Expense();
food.Name = "Food";
food.Value = 50;
sliderFood.DataContext = food;
expenses.Add(food);
Expense rent = new Expense();
rent.Name = "Rent";
rent.Value = 100;
sliderRent.DataContext = rent;
Expense renterInsurance = new Expense();
renterInsurance.Name = "Renter's Insurance";
renterInsurance.Value = rent.Value * 0.1;
expenses.Add(renterInsurance);
ps.ItemsSource = expenses;
}
}
XAML:
<Grid x:Name="LayoutRoot" Background="White" Height="700" Width="600">
<Canvas Height="700" Width="600">
<toolkit:Chart Name="exampleChart" Height="300" Width="300" Canvas.Left="150" Canvas.Top="5">
<toolkit:Chart.Series>
<toolkit:PieSeries IndependentValueBinding="{Binding Name}" DependentValueBinding="{Binding Value}" />
</toolkit:Chart.Series>
</toolkit:Chart>
<Slider Canvas.Left="11" Canvas.Top="65" Height="23" HorizontalAlignment="Left" Margin="158,254,0,0" Name="sliderFood" VerticalAlignment="Top" Width="100" Minimum="0" Maximum="100"
Value="{Binding Value, Mode=TwoWay}"/>
<Slider Height="23" HorizontalAlignment="Left" Margin="158,12,0,0" Name="sliderRent" VerticalAlignment="Top" Width="100" Minimum="0" Maximum="100" Canvas.Left="154" Canvas.Top="307"
Value="{Binding Value,Mode=TwoWay}"/>
</Canvas>
</Grid>