none
ConverterParameter能传递某个控件的属性吗 RRS feed

  • 问题

  • 比如有个TextBlock显示一个按钮的Margin属性,通过实现IValueConverter接口的转换类,把该Button的Margin属性进去

    显示结果为: LEFT: xxx, TOP: yyy, RIGHT: zzz, BOTTOM: mmm

    2017年8月10日 12:27

答案

  • Hi,

    你在绑定的时候使用转化器,既然TextBlock的Text属性已经绑定了Button的Margin属性,为什么还需要传递参数?

    而且,converterparameter不是依赖属性,所以不能用binding。

      <Window.Resources>
            <local:MyConverter x:Key="MyConverter"/>
        </Window.Resources>
        <StackPanel>
            <Button x:Name="mybutton" Margin="2,4,7,1" Content="Test"></Button>
            <TextBlock Text="{Binding ElementName=mybutton, Path=Margin, Converter= {StaticResource MyConverter}}"></TextBlock>
        </StackPanel>


     public class MyConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                string val = value.ToString();
                string[] sArray = val.Split(',');
                 return String.Format("Left:{0}, Top:{1}, Right:{2}, Botton:{3}",sArray[0], sArray[1],sArray[2],sArray[3]);
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2017年8月11日 2:48
    版主
  • Hi,

    >> 应该是TextBlock显示按钮的Content和Margin,绑定到按钮的Content,然后按钮的Margin作为参数传递进去

    这样也不行,因为converterparameter不是依赖属性,不能用binding。如果你想绑定至少两个属性,那你就需要使用MultiBinding,然后使用IMultiValueConverter来转成你想要的结果。

       <TextBlock Margin="5" >
                    <TextBlock.Text>
                        <MultiBinding Converter="{StaticResource MyMultiValueConverter}">
                            <Binding ElementName="mybutton" Path="Content" />
                            <Binding ElementName="mybutton" Path="Margin" />
                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>
    public class MyMultiValueConverter : IMultiValueConverter
        {
            public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
            {
                string one = values[0] as string;
                string two = values[1].ToString();
                if (!string.IsNullOrEmpty(one) && !string.IsNullOrEmpty(two))
                {
                    string[] sArray = two.Split(',');
                    return String.Format("Content:{0}  Margin( Left:{1} Top:{2} Right:{3} Botton:{4})", one, sArray[0], sArray[1], sArray[2], sArray[3]);
                }
                return null;
            }
    
            public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2017年8月11日 4:39
    版主

全部回复

  • Hi,

    你在绑定的时候使用转化器,既然TextBlock的Text属性已经绑定了Button的Margin属性,为什么还需要传递参数?

    而且,converterparameter不是依赖属性,所以不能用binding。

      <Window.Resources>
            <local:MyConverter x:Key="MyConverter"/>
        </Window.Resources>
        <StackPanel>
            <Button x:Name="mybutton" Margin="2,4,7,1" Content="Test"></Button>
            <TextBlock Text="{Binding ElementName=mybutton, Path=Margin, Converter= {StaticResource MyConverter}}"></TextBlock>
        </StackPanel>


     public class MyConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                string val = value.ToString();
                string[] sArray = val.Split(',');
                 return String.Format("Left:{0}, Top:{1}, Right:{2}, Botton:{3}",sArray[0], sArray[1],sArray[2],sArray[3]);
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2017年8月11日 2:48
    版主
  • Hi,

    你在绑定的时候使用转化器,既然TextBlock的Text属性已经绑定了Button的Margin属性,为什么还需要传递参数?

    而且,converterparameter不是依赖属性,所以不能用binding。

      <Window.Resources>
            <local:MyConverter x:Key="MyConverter"/>
        </Window.Resources>
        <StackPanel>
            <Button x:Name="mybutton" Margin="2,4,7,1" Content="Test"></Button>
            <TextBlock Text="{Binding ElementName=mybutton, Path=Margin, Converter= {StaticResource MyConverter}}"></TextBlock>
        </StackPanel>


     public class MyConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                string val = value.ToString();
                string[] sArray = val.Split(',');
                 return String.Format(",sArray[0], sArray[1],sArray[2],sArray[3]);
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    我想了一下,我描述有问题

    应该是TextBlock显示按钮的Content和Margin,绑定到按钮的Content,然后按钮的Margin作为参数传递进去。

    2017年8月11日 4:18
  • Hi,

    >> 应该是TextBlock显示按钮的Content和Margin,绑定到按钮的Content,然后按钮的Margin作为参数传递进去

    这样也不行,因为converterparameter不是依赖属性,不能用binding。如果你想绑定至少两个属性,那你就需要使用MultiBinding,然后使用IMultiValueConverter来转成你想要的结果。

       <TextBlock Margin="5" >
                    <TextBlock.Text>
                        <MultiBinding Converter="{StaticResource MyMultiValueConverter}">
                            <Binding ElementName="mybutton" Path="Content" />
                            <Binding ElementName="mybutton" Path="Margin" />
                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>
    public class MyMultiValueConverter : IMultiValueConverter
        {
            public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
            {
                string one = values[0] as string;
                string two = values[1].ToString();
                if (!string.IsNullOrEmpty(one) && !string.IsNullOrEmpty(two))
                {
                    string[] sArray = two.Split(',');
                    return String.Format("Content:{0}  Margin( Left:{1} Top:{2} Right:{3} Botton:{4})", one, sArray[0], sArray[1], sArray[2], sArray[3]);
                }
                return null;
            }
    
            public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2017年8月11日 4:39
    版主