2017-06-27 42 views
0

我有我自己的組合框(autocompleteCombobox),我希望只看到selectedItem的35個字符,但帶有顯示全名的工具提示。如何修剪ComboBox中顯示的文本?

用戶控件代碼:

<UserControl.Resources> 
    <Style x:Key="ComboboxStyle" TargetType="{x:Type ComboBox}"> 
     <Setter Property="ItemTemplate"> 
      <Setter.Value> 
       <DataTemplate> 
        <TextBlock Text="{Binding ShownName}"/> 
       </DataTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</UserControl.Resources> 
<Grid> 
    <ComboBox x:Name="Combobox" IsSynchronizedWithCurrentItem="True" 
       IsEditable="True" TextSearch.Text="ShownName" 
       IsTextSearchEnabled="True" DisplayMemberPath="ShownName" 
       ToolTip="{Binding SelectedItem.ShownName,ElementName=autoComplete}" 
       ItemsSource="{Binding ItemsSource,ElementName=autoComplete}" 
       SelectedItem="{Binding SelectedItem, ElementName=autoComplete}" 
       Style="{StaticResource ComboboxStyle}"> 
     <ComboBox.InputBindings> 
      <KeyBinding Key="Enter" 
         Command="{Binding Path=SelectItemCommand, ElementName=autoComplete}" 
         CommandParameter="ShownName"/> 
     </ComboBox.InputBindings> 
    </ComboBox> 
</Grid> 

而且autocompletecombobox的CS文件中:

public static readonly DependencyProperty MaxTextLengthProperty = 
     DependencyProperty.Register(
      "MaxTextLength", 
      typeof(int), 
      typeof(ComboBoxAutoComplete), 
      new UIPropertyMetadata(35)); 
     public int MaxTextLength 
     { 
      get { return (int)GetValue(MaxTextLengthProperty); } 
      set 
      { 
       SetValue(MaxTextLengthProperty, value); 
       LimitTextInCombobox(); 
      } 
     } 

     private void LimitTextInCombobox() 
     { 
      Combobox.Text = Combobox.Text.Substring(0, MaxTextLength); 
     }  

但它不工作...

+0

有關創建一個新的'IValueConverter',而不是一個新的'DependecyProperty'的限制是什麼文本? – GeorgeChond

+0

你能告訴我它是如何工作的嗎? – Krom

+0

我在嘗試,但如果您將文本轉換爲限制文本,則工具提示也受到限制... – Krom

回答

2

相反比修剪文字到一定數量的字符,你可以讓WPF用re修剪它例如文本的可視寬度,這可能會更好看。如果這是您的選擇,您可以查看TextBlock.TextTrimming屬性。

+1

開頭的項目@Krom您可以給它一個確實有ItemTemplate的ItemTemplate。 –

+1

@Krom使用你自己的模板對任何一個模板都沒有任何影響。如果你需要編寫模板的幫助,我可以告訴你如何。 –

+0

它可能是有用的,但如果我把textTrimming放在文本塊中,那麼所有的項目都會被裁剪掉,而我只想將組合框中顯示的項目修剪爲「selected」,而不是其他的元素列表(當列表是下拉列表時,這些項目必須有其正常長度) – Krom

1

您可以使用轉換器

[ValueConversion(typeof(object), typeof(string))] 
public class StringFormatConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, 
          object parameter, CultureInfo culture) 
    { 
     string str = value.ToString(); 
     if (string.IsNullOrEmpty(str)) 
     { 
      return ""; 
     } 
     else 
     { 
      if (str.Length >= 35) 
      { 
       return str.Substring(0, 35); 
      } 
      else 
      { 
       return str; 
      } 

     } 

    } 


    public object ConvertBack(object value, Type targetType, 
           object parameter, CultureInfo culture) 
    { 
     throw new NotSupportedException(); 
    } 
} 

,並在XAML

<Windows.Resources> 
    <dict:StringFormatConverter x:Key="StringFormatConverter"/> 
</Windows.Resources> 

<UserControl.Resources> 
<Style x:Key="ComboboxStyle" TargetType="{x:Type ComboBox}"> 
    <Setter Property="ItemTemplate"> 
     <Setter.Value> 
      <DataTemplate> 
       <TextBlock Text="{Binding ShowName, Converter={StaticResource StringFormatConverter}}"/> 
      </DataTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

+0

但用這個解決方案,所有的項目將被修剪...我只想要顯示,但工具提示必須顯示全名 – Krom

1

一個CLR包裝的依賴項屬性的setter應該始終呼叫SetValue方法來設置依賴項屬性的值。沒有別的:

public int MaxTextLength 
{ 
    get { return (int)GetValue(MaxTextLengthProperty); } 
    set 
    { 
     SetValue(MaxTextLengthProperty, value); 
    } 
} 

此外,你想保持原始值能夠顯示它在工具提示。

使用@Alematt建議的轉換器看起來是一個不錯的選擇。只需修改ItemTemplate略:

​​

,並創建一個轉換器類:

public class Converter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     string shownName = value as string; 
     if (!string.IsNullOrEmpty(shownName) && shownName.Length > 35) 
      return shownName.Substring(0, 35) + "..."; 

     return value; 

    } 

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

而且保持Tooltip,因爲它是:

ToolTip="{Binding SelectedItem.ShownName, ElementName=autoComplete}" 
+0

我試過你的解決方案,它修剪的字符串,但只有組合框是下拉:DI會喜歡修剪組合框中顯示的值(所選項目) – Krom

+0

然後,您應該將IsEditable屬性設置爲false。 – mm8

+0

我得出了同樣的結論...我真的沒有找到任何方式使用textsearch選項和啓用的選項... – Krom

1

取而代之的是剛剛通過的SelectedItem到您的轉換器像這樣:

 <ComboBox x:Name="cmb"> 
     <ComboBox.Style> 
      <Style TargetType="ComboBox"> 
       <Setter Property="ItemTemplate"> 
        <Setter.Value> 
         <DataTemplate> 
          <TextBlock> 
           <TextBlock.Text> 
            <MultiBinding Converter="{StaticResource multi}"> 
             <Binding Path="."/> 
             <Binding Path="SelectedItem" ElementName="cmb"/> 
            </MultiBinding> 
           </TextBlock.Text> 
          </TextBlock> 
       </DataTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
     </ComboBox.Style> 
     <ComboBox.Items> 
      <sys:String>Very long string 1</sys:String> 
      <sys:String>Very long string 2</sys:String> 
     </ComboBox.Items> 
    </ComboBox> 

然後用你的轉換器,像這樣:

class MultiValConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (values[0] != null && values[1] != null) 
     { 
      if (values[0].ToString() == values[1].ToString()) 
      { 
       return "...";//put your logic here i.e. substring(0,30); 
      } 
     } 
     return values[0]; 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

,你會引用這樣的轉換器:

<locals:MultiValConverter x:Key="multi"/>