Combox 不会在 WPF 中更新

本文关键字:更新 WPF Combox | 更新日期: 2023-09-27 18:36:02

我使用以下代码Bind ComboBox DistanceRoundoffs列表的ItemsSource

我还将ComboBoxSelectedItem绑定到RebarsVerticalDistanceRoundoff财产上。

<ComboBox ItemsSource="{Binding Path=DistanceRoundoffs}"
          SelectedItem="{Binding SettingsViewModel.RebarsVerticalDistanceRoundoff, 
                    RelativeSource={RelativeSource FindAncestor, AncestorType=Window}, 
                    Mode=TwoWay}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Converter={StaticResource LengthConverter}}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
private List<double> distanceRoundoffs = new List<double> { 25, 50};
public List<double> DistanceRoundoffs
{
    get { return distanceRoundoffs; }
    set
    {
        distanceRoundoffs = value;
        RaisePropertyChanged("DistanceRoundoffs");
    }
}
private double rebarsVerticalDistanceRoundoff;
public double RebarsVerticalDistanceRoundoff
{
    get { return rebarsVerticalDistanceRoundoff; }
    set
    {
        rebarsVerticalDistanceRoundoff = value;
        RaisePropertyChanged("RebarsVerticalDistanceRoundoff");
    }
}

我还实现了一个IValueConverter,将ComboBox的值转换为另一个单位。转换器接受double值,并根据名为 lfactor 的参数更改其单位。

public class LengthConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var length = (double)value;
        var lfactor = Building.LengthFactor;
        return string.Format("{0}",length / lfactor);
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }
}

当我们通过下面列出的另一个组合框更改单位时,将获得lfactor参数。

private void Units_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    GetLengthFactor();
    // Building.LengthFactor is changed here! Later used in LengthConverter
}

距离舍入的初始值以mm为单位:25,50

当我更改UnitComboBox时,Units_OnSelectionChanged触发,但DistanceRoundoffs没有更新。

Combox 不会在 WPF 中更新

你原来使用多重绑定的方法还不错。您只需要确保各个绑定正确即可。在 particalur 中,"单位因子"绑定需要指定其源对象。

假设同一视图模型中存在保存 DistanceRoundoffs 属性的 LengthFactor 属性,则 DataTemplate 将如下所示:

<ComboBox.ItemTemplate>
    <DataTemplate>
        <TextBlock>
            <TextBlock.Text>
                <MultiBinding Converter="{StaticResource LengthConverter}">
                    <Binding Path="."/>
                    <Binding Path="DataContext.LengthFactor"
                        RelativeSource="{RelativeSource AncestorType=ComboBox}"/>
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </DataTemplate>
</ComboBox.ItemTemplate>

LengthConverter 现在实现了IMultiValueConverter,其 Convert 方法如下所示:

public object Convert(
    object[] values, Type targetType, object parameter, CultureInfo culture)
{
    return ((double)values[0] *(double)values[1]).ToString();
}

WPF 不知道所选单位与距离舍入显示之间的关系。只要没有信号表明DistanceRoundoffs已更改,它就没有理由更新该组合框。

因此,您必须通过引发PropertyChanged事件来给它一个信号,以便在所选单位发生更改时DistanceRoundoffs,例如在所选单位属性的 setter 中。