2013-08-07 77 views
0

我想修剪Silverlight自定义控件组合框的选定值。我发现使用IValueConverter类应该是一条路。所以,我在图书馆如何将资源添加到Silverlight自定义控件?

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(); 
    } 
} 

创建此这对组合框

<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> 

除了资源“StringTrimmer”,为的SelectedItem解决不了的XAML。我试着将这个引用添加到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是什么microsof不得不说的XAML命名空间

和这http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.convert.aspx是为IValueConverter

我哪里错了?

回答

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> 

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

相关问题