Combox 不会在 WPF 中更新
Combox doesn't get updated in WPF
我使用下面的代码 Bind
ComboBox
到 DistanceRoundoffs
列表的 ItemsSource
。
我还绑定了 ComboBox
的 SelectedItem
到 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
}
DistanceRoundoffs 的初始值在 mm
: 25,50
当我更改 UnitComboBox 时,Units_OnSelectionChanged
会触发,但 DistanceRoundoffs
不会更新。
WPF 不知道所选单位与显示的距离四舍五入之间的关系。只要没有信号表明 DistanceRoundoffs
已更改,它就认为没有理由更新该组合框。
因此,当所选单位发生更改时,您必须通过为 DistanceRoundoffs
引发 PropertyChanged
事件来向其发出信号,例如在您选择的单位 属性 的 setter 中。
您使用 MultiBinding 的原始方法非常好。您只需确保各个绑定是正确的。特别是,"unit factor" 绑定需要指定其源对象。
假设在包含 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();
}
我使用下面的代码 Bind
ComboBox
到 DistanceRoundoffs
列表的 ItemsSource
。
我还绑定了 ComboBox
的 SelectedItem
到 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
}
DistanceRoundoffs 的初始值在 mm
: 25,50
当我更改 UnitComboBox 时,Units_OnSelectionChanged
会触发,但 DistanceRoundoffs
不会更新。
WPF 不知道所选单位与显示的距离四舍五入之间的关系。只要没有信号表明 DistanceRoundoffs
已更改,它就认为没有理由更新该组合框。
因此,当所选单位发生更改时,您必须通过为 DistanceRoundoffs
引发 PropertyChanged
事件来向其发出信号,例如在您选择的单位 属性 的 setter 中。
您使用 MultiBinding 的原始方法非常好。您只需确保各个绑定是正确的。特别是,"unit factor" 绑定需要指定其源对象。
假设在包含 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();
}