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

本文关键字:绑定 问题 列表 标签 | 更新日期: 2023-09-27 18:36:16

所以我以前做过绑定,我不确定这次我的绑定出了什么问题。有人可以向我解释为什么我不能将标签绑定到适当的属性吗?列表框完美运行且符合预期,但我不知道为什么标签不起作用。

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" });

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

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

无论如何,在您向我们展示的代码中,如果属性是经济度量,则在OnPropertyChanged中设置metricName和metricUnit。但是您从未在任何地方设置 EconomyMetric,因此永远不会设置绑定属性。您需要更好地了解您的代码。

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

<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>