【问题标题】:Silverlight: binding to the result of a calculation that involes a propertySilverlight:绑定到涉及属性的计算结果
【发布时间】:2013-04-03 14:59:18
【问题描述】:

我想做的不仅是将属性 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>

【问题讨论】:

    标签: c# xaml silverlight data-binding


    【解决方案1】:

    您需要修改费用类别,以便它可以订阅其他费用类别的 propertyChanged 事件,这样您就可以将租房者保险连接到租金类别,并让它在看到该租金时更新自己的值。值变化。

    我相信这条线应该可行。您可以将其添加到构造函数的底部。

    rent.PropertyChanged += (sender, args) => {
      if (args.PropertyName == "Value")
        renterInsurance.Value = ((Expense)sender).Value * 0.1;
    };
    

    【讨论】:

    • 谢谢马尔科姆。你能指出一些“订阅”是如何完成的例子吗?我是新手。
    • 感谢您的快速回复。当我今晚晚些时候可以回去工作时,我会试一试。仍在试图解决这个问题 - 如果我将这一行放在 Expense 类的定义中,不会有任何错误,因为此时既不存在“租金”也不存在“renterInsurance”?
    • - 好吧,我猜你的意思是把它放在 MainPage() 中。
    • 是的,对不起,我的意思不是 MainPage 的构造函数,而不是 Expense。
    • 马尔科姆,我可以试一试,效果很好。再次感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多