Tom
Tom

Reputation: 1344

Clear whitespace from end of string in WPF/XAML

I have an MVVM application that uses a listbox which is populated with images. The image string always comes from an object that I can't modify because it's generated using an edmx model.

To cut a story shory, I need to put into the following xaml a way to trim the whitespace put onto the end of the image path by SQL from the string.

<ListBox ItemsSource="{Binding AllImages}" x:Name="listBox1" Width="300" Margin="10,10,0,10">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Image Grid.Column="0" Source="{Binding imagePath}" Height="100" Width="100" />
                <TextBlock Grid.Column="1" Text="{Binding imageId}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Is this possible?

Upvotes: 6

Views: 7681

Answers (2)

MyKuLLSKI
MyKuLLSKI

Reputation: 5325

If you do not want to use a converter your can do it right into your Property

INotifyChangedProperty Solution

private string _ImageID;
public string ImageID
{
    get
    {
        return _ImageID;
    }

    set
    {
       value = (value == null ? value : value.Trim());
       NotifyPropertyChanged("ImageID");
    }
}

DependencyProperty Solution

public static readonly DependencyProperty ImageIDProperty =
    DependencyProperty.Register("ImageID", typeof(string), typeof(MainWindowViewModel), new PropertyMetadata(string.Empty));

    public string ImageID
    {
        get { return (string)GetValue(ImageIDProperty); }
        set { SetValue(ImageIDProperty, value == null ? value : value.Trim()); }
    }

Upvotes: 5

brunnerh
brunnerh

Reputation: 185117

Use a value converter in the binding which does the trimming for you.

Upvotes: 8

Related Questions