簡體   English   中英

如何在XAML中為組合框定義值轉換器

[英]How to define a value converter for a combobox in XAML

我正在嘗試將DistanceRoundoffs列表的值加載到ComboBox 值以mm但我想以cm為單位顯示,因此需要使用值轉換器。

我不知道如何以及在哪里使用它。 我應該在ItemsSourceSelectedItem定義它嗎?

我不需要價值轉換器的代碼; 只是當前組合框在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");
    }
}

您應該在ComboBox的ItemTemplate中使用轉換器:

<ComboBox ...>
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Converter={StaticResource UnitConverter}}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM