Rami ZK
Rami ZK

Reputation: 580

how to trim binding data using string format

I have a bounded databases with datagridview and I'm trying to trim the bounded string with stringFormat but I didn't find out how to do it . (I won't to make a property in c# file and trim it)

<DataGridTextColumn  Binding="{Binding type}"    Width="100" HeaderStyle="{StaticResource HeaderStyle}"   ElementStyle="{StaticResource dataGridElementStyle}"  />

Upvotes: 2

Views: 5333

Answers (3)

mm8
mm8

Reputation: 169330

XAML is a markup language. It really has no way of trimming strings. Applying a StringFormat is not the same thing as being able to call some method (like Trim()) to manipulate the string and you cannot call methods in XAML.

You should either use a value converter to trim the value as suggested here:

Clear whitespace from end of string in WPF/XAML

Or you should return an already trimmed string from your source property.

Edit: If you refer to trimming in the context of being able to draw an ellipsis in place of the remaining text when the length of the text overflows the available content area you could set the TextTrimming property of the ElementStyle as suggested by Chris W.

Upvotes: 0

Chris W.
Chris W.

Reputation: 23290

You have multiple ways to accomplish this. You can create a custom CellStyle, you could set a Style Template in the DataGrid.Resources to hit the TargetType of TextBlock through the whole DataGrid (which is what DataGridTextColumn renders as at runtime). Generally though I do something like this if you only want it on the individual instance which is what I assume you're trying to do.

<DataGridTextColumn Binding="{Binding type}" Width="100">
   <DataGridTextColumn.ElementStyle>
      <!-- Since it's already set as "NoWrap" by default, 
           just need to set trimming which will be invoked 
           by your set width you already have. -->
      <Style TargetType="{x:Type TextBlock}">
         <Setter Property="TextTrimming" Value="CharacterEllipsis"/>
      </Style>
   </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

or like this;

  <DataGridTemplateColumn>
     <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
           <TextBlock TextTrimming="Character Ellipses"
                      Text="{Binding type}"/>
        </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

..or other ways. Just have to hit the end result TextBlock one way or another.

Also since you already have an ElementStyle designated as a resource, you could add the example to it, although I stripped it from this example to have it inline. Would need to see your resource for me to show you with it.

Hope this helps, happy holidays.

Upvotes: 1

Karel Tamayo
Karel Tamayo

Reputation: 3760

You have a few options to achieve want you want. Unfortunately, you can't trim text using string format expression in binding. That said, I'll give you some options but I think the simplest is the property you said you won't create.

Option 1: Use a converter: You could create a converter and use it in the binding:

TrimTextConverter: IValueConverter{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        //Trim your text here
    }   
}

PROS: It's readable when you use it, you can clearly see the intent: <DataGridTextColumn Binding="{Binding type, Converter={StaticResource TrimTextConverter}}" .../>

CONS::

  1. You need to be careful because if you set your UpdateSourceTrigger=PropertyChanged in your binding this converter will prevent you from add any space.
  2. You need to create another class just for this functionality.

Option 2: Create you own DataGridTrimmedTextColumn (inheritance):

This way you can inherit the DataGridTextColumn and implement the feature of trimming. So much work in this case (again).

Option 3: Bind to property with a trimmed value:

The property you didn't want to create. But as I said at the beginning maybe the simplest approach.

Hope this helps!

Upvotes: 0

Related Questions