0

我正在尝试修剪 silverlight 自定义控件组合框的选定值。我发现使用 IValueConverter 类应该是要走的路。所以我在我的图书馆里创造了这个

public class StringTrimmer : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.ToString().Trim();
    }

    public object ConvertBack(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

这是组合框的 xaml

<UserControl x:Class="SilverlightControlLibrary.SilverlightComboBoxControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Height="25" Width="122">
<ComboBox Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120"
          Margin="0,0,0,0" Name="ModuleIDFilterCB" 
          ItemsSource="{Binding Path=Screen.LicenseModuleIDs, Mode=TwoWay}"
          DisplayMemberPath="LicenseModuleName"      
          SelectedItem="{Binding Screen.bufferProp, Converter={StaticResource StringTrimmer},Mode=TwoWay}"
          />
</UserControl>

除了无法解析 SelectedItem 的资源“StringTrimmer”。我尝试将此引用添加到 xaml,但它仍然没有工作。

xmlns:c="clr-namespace:SilverlightControlLibrary"

编辑:我也试过这个

xmlns:custom="clr-namespace:SilverlightControlLibrary"

随着

SelectedItem="{Binding Screen.bufferProp, Converter= {StaticResource custom:StringTrimmer}, Mode=TwoWay}"

无济于事。。

这个http://msdn.microsoft.com/en-us/library/cc189061%28v=vs.95%29.aspx是微软对 XAML 命名空间的看法

这个http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.convert.aspx用于 IValueConverter

我哪里错了?

4

1 回答 1

0

您必须添加静态资源(UserControl.Resources部分)来引用您的转换器,例如:

<UserControl 
    x:Class="SilverlightControlLibrary.SilverlightComboBoxControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" 
    Height="25" 
    Width="122"
    xmlns:custom="clr-namespace:SilverlightControlLibrary">

<UserControl.Resources>
   <custom:StringTrimmer x:Key="StringTrimmer" />
</UserControl.Resources>

<ComboBox Height="23" 
          HorizontalAlignment="Left" 
          VerticalAlignment="Top" 
          Width="120"
          Margin="0,0,0,0" 
          Name="ModuleIDFilterCB" 
          ItemsSource="{Binding Path=Screen.LicenseModuleIDs, Mode=TwoWay}"
          DisplayMemberPath="LicenseModuleName"      
          SelectedItem="{Binding Screen.bufferProp, Converter={StaticResource StringTrimmer}, Mode=TwoWay}" />
</UserControl>

对于 XAML 参考,您可以在x:Key属性中使用任何名称,而不仅仅是“StringTrimmer”。

于 2013-08-07T20:44:22.110 回答