WPF binding multi-valued and multi-value conversion (MultiBinding and IMultiValueConverter)

WPF can use MultiBinding multi-value binding, multi-value conversion using IMultiValueConverter

Example:

(1) converter

    ContentConverter class public: IMultiValueConverter 
    { 
        // the source attributes to the target properties, this method is called ConvertBack 
        public the Convert Object (Object [] values, the targetType the Type, Parameter Object, the CultureInfo Culture) 
        { 
            IF (values.Length values == null || 0 ==) 
                the throw ArgumentNullException The new new ( "CAN Not BE null values"); 

            String S = ""; 
            the foreach (values in Item var) 
            { 
                S = + System.Convert.ToString (Item); 
            } 

            return S; 
        } 

        // when the target attribute to a source properties, this method is called ConvertBack
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

(2) Binding

    <Window.Resources>
        <local:ContentConverter x:Key="content"></local:ContentConverter>
    </Window.Resources>
    <Grid>
        <Label HorizontalAlignment="Left" Height="23" Margin="243,208,0,0" Foreground="{Binding Status,Converter={StaticResource foreColor},Mode=OneWay}" VerticalAlignment="Top" Width="120">
            <Label.Content>
                <MultiBinding Converter="{StaticResource content}">
                    <Binding Path="Str1"/>
                    <Binding Path="Str2"/>
                    <Binding Path="Str3"/>
                </MultiBinding>
            </Label.Content>
        </Label>
        <Button Content="Button" HorizontalAlignment="Left" Margin="35,44,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    </Grid>

(3) button click event

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            window2ViewModel.Str1 = "1";
            window2ViewModel.Str2 = "2";
            window2ViewModel.Str3 = "3";
        }

(4) the effect lable string display str1, str2 and adding str3

 

 

  

 

Guess you like

Origin www.cnblogs.com/yaosj/p/11240275.html