我尝试将DistanceRoundoffs
列表的值加载到ComboBox
。这些值在mm
中,但我想在cm
中显示它们,因此我需要使用值转换器。
我不知道如何以及在何处使用它。我应该在ItemsSource
还是SelectedItem
中定义它?
我不需要价值转换器的代码;只是在XAML中实现当前的组合框。
<ComboBox ItemsSource="{Binding Path=DistanceRoundoffs}"
SelectedItem="{Binding DistanceRoundoff,
RelativeSource={RelativeSource FindAncestor, AncestorType=Window},
Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource MultiUnitConverter}" ConverterParameter="{x:Static enumerations:Quantity.Length}">
<Binding Path="RebarsVerticalDistanceRoundoff"/>
<Binding Path="CurrentTargetUnit"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</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 distanceRoundoff;
public double DistanceRoundoff
{
get { return distanceRoundoff; }
private set
{
distanceRoundoff= value;
RaisePropertyChanged("DistanceRoundoff");
}
}
答案 0 :(得分:2)
您应该在ComboBox的ItemTemplate
中使用转换器:
<ComboBox ...>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource UnitConverter}}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>