绑定标签但不是列表框的问题

时间:2016-02-26 11:57:46

标签: c# wpf binding

所以我之前已经完成了绑定,而且我不确定这次绑定有什么问题。有人可以向我解释为什么我不能将我的标签绑定到适当的属性?列表框完美且按预期工作,但我不知道为什么标签不起作用。

XAML

<Label Content="{Binding MetricName}" Height="25"></Label>
    <Label Content="{Binding MetricUnit}" Height="25"></Label>
    <ListBox x:Name="EconomicSelection" Grid.Column="1" HorizontalAlignment="Left" Height="150" Margin="5,5,0,5" VerticalAlignment="Top" Width="399" FontFamily="{DynamicResource FontFamily}" FontSize="11" ItemsSource="{Binding EconomicBenchmarks}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Label Content="P" Grid.Column="0"/>
                    <TextBox Text="{Binding Percentile}" Grid.Column="0" Width="30"/>
                    <Label Content="Value: " Grid.Column="1"/>
                    <TextBox Text="{Binding Value}" Grid.Column="1"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

C#

private Metric economicMetric;

    public Metric EconomicMetric
    {
        get { return economicMetric; }
        set
        {
            if (value == economicMetric) return;
            economicMetric = value;
            OnPropertyChanged();
        }
    }

    private string metricName;

    public string MetricName
    {
        get { return metricName; }
        set
        {
            if (value == metricName) return;
            metricName = value;
            OnPropertyChanged();
        }
    }

    private string metricUnit;

    public string MetricUnit
    {
        get { return metricUnit; }
        set
        {
            if (value == metricUnit) return;
            metricUnit = value;
            OnPropertyChanged();
        }
    }

    private ObservableCollection<EconomicBenchmark> economicBenchmarks = new ObservableCollection<EconomicBenchmark>();

    public ObservableCollection<EconomicBenchmark> EconomicBenchmarks
    {
        get { return economicBenchmarks; }            
    }

    public EconomicMeasuresTable()
    {
        InitializeComponent();
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public event EconomicMeasuresChangedEventHandler EconomicMeasuresChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        if (propertyName == "EconomicMetric")
        {
            metricName = economicMetric.EconomicName;
            metricUnit = economicMetric.Unit;
            economicBenchmarks.Clear();
            foreach (var economicBenchmark in economicMetric.EconomicBenchmarks)
            {
                economicBenchmarks.Add(economicBenchmark);
            }            
        }
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));           
    }

    private void SetButton_Click(object sender, RoutedEventArgs e)
    {
        if (EconomicMeasuresChanged != null)
        {
            EconomicMeasuresChanged(this, new EventArgs());
        }
    }
}

public delegate void EconomicMeasuresChangedEventHandler(object sender, EventArgs e);

列表

List<Metric> metrics = new List<Metric>();

        metrics.Add(new Metric { EconomicItem = "NVP", EconomicName = "Net Present Value", EconomicBenchmarks = GetEconomicBenchmarks(new[] { 10, 50, 90 }, new[] { 400, 550, 700 }), Unit = "$m" });
        metrics.Add(new Metric { EconomicItem = "ROI", EconomicName = "Return On Investment", EconomicBenchmarks = GetEconomicBenchmarks(new[] {10, 50, 90}, new[] {10, 20, 30}), Unit = "%" });
        metrics.Add(new Metric { EconomicItem = "STOIIP", EconomicName = "STOIIP", EconomicBenchmarks = GetEconomicBenchmarks(new[] {10, 50, 90}, new[] {500, 550, 600}), Unit = "MMbbl" });

2 个答案:

答案 0 :(得分:1)

你在这段代码中所做的事情可能是不完整的,有点奇怪。首先,我永远不会使用OnPropertyChanged设置任何值,但也许这只是我的意见......

无论如何,在您向我们展示的代码中,如果property为EconomicMetric,则在OnPropertyChanged中设置metricName和metricUnit。但是你从不在任何地方设置EconomyMetric,因此永远不会设置绑定属性。您需要更好地查看代码。

答案 1 :(得分:1)

我从您的代码中了解到的是Metric类包含 EconomicBenchmarks 集合,那么为什么不直接将该集合与这样的视图绑定,

<ListBox x:Name="EconomicSelection" Grid.Column="1" HorizontalAlignment="Left" Height="150" Margin="5,5,0,5" VerticalAlignment="Top" Width="399" FontSize="11" ItemsSource="{Binding EconomicMetric.EconomicBenchmarks}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Label Content="P" Grid.Column="0"/>
                        <TextBox Text="{Binding Percentile}" Grid.Column="0" Width="30"/>
                        <Label Content="Value: " Grid.Column="1"/>
                        <TextBox Text="{Binding Value}" Grid.Column="1"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>