5

I want to append two static strings for a single content or header of a WPF object. Something like this:

<MenuItem 
    Header="{x:Static properties:Resources.SEARCH_FOR_DAYS} + 
            {x:Static properties:Resources.ELLIPSES}" /> 

I've played around with ContentStringFormat and the like but can't get it to accept two resources.

3 Answers 3

5
<MenuItem>
    <MenuItem.Header>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{x:Static properties:Resources.SEARCH_FOR_DAYS}" />
            <TextBlock Text="{x:Static properties:Resources.ELLIPSES}" />
        </StackPanel>
    </MenuItem.Header>
</MenuItem>

Alternatively (closer to what you requested):

<MenuItem>
    <MenuItem.Header>
        <MultiBinding StringFormat="{}{0}{1}">
            <Binding Path="{x:Static properties:Resources.SEARCH_FOR_DAYS}"/>
            <Binding Path="{x:Static properties:Resources.ELLIPSES}"/>
        </MultiBinding>
    </MenuItem.Header>
</MenuItem>    
Sign up to request clarification or add additional context in comments.

2 Comments

I went with your first response because it seems the easiest to understand for another developer. Thank you!
That’s the one I typically use as well (especially when I need to throw in other UI elements, such as small icons). But I would be interested in trying out Tim’s suggestion (+1), since it might be more lightweight.
4

Off the top of my head, you might be able to do:

<MenuItem>
    <MenuItem.Header>
        <TextBlock>
            <Run Text="{x:Static properties:Resources.SEARCH_FOR_DAYS}" />
            <Run Text="{x:Static properties:Resources.ELLIPSES}" />
        </TextBlock>
    </MenuItem.Header>
</MenuItem>

Comments

0

when you disable the MenuItem in this code:

<MenuItem IsEnabled="False">
    <MenuItem.Header>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{x:Static properties:Resources.SEARCH_FOR_DAYS}" />
            <TextBlock Text="{x:Static properties:Resources.ELLIPSES}" />
        </StackPanel>
    </MenuItem.Header>
</MenuItem>

the text doesn't become gray.

But if you make the same thing in this other code:

<MenuItem IsEnabled="False">
    <MenuItem.Header>
        <StackPanel Orientation="Horizontal">
            <Label Padding="0" Content="{x:Static properties:Resources.SEARCH_FOR_DAYS}" />
            <Label Padding="0" Content="{x:Static properties:Resources.ELLIPSES}" />
        </StackPanel>
    </MenuItem.Header>
</MenuItem>

Enable and disable works as expected on the color of the text

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.