0

私がやりたいのは、プロパティ 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>
4

1 に答える 1

0

費用クラスを変更して、他の費用クラスの propertyChanged イベントをサブスクライブできるようにする必要があります。これにより、賃借人の保険を家賃クラスに接続し、rent.value の変更を確認したときに独自の値を更新できるようになります。

この行は機能するはずだと思います。コンストラクターの一番下に追加できます。

rent.PropertyChanged += (sender, args) => {
  if (args.PropertyName == "Value")
    renterInsurance.Value = ((Expense)sender).Value * 0.1;
};
于 2013-04-03T15:04:54.210 に答える