Append WPF resource strings
Asked Answered
A

3

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.

Auraaural answered 16/5, 2012 at 17:22 Comment(0)
P
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>    
Prosper answered 16/5, 2012 at 17:26 Comment(2)
I went with your first response because it seems the easiest to understand for another developer. Thank you!Auraaural
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.Prosper
I
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>
Intercurrent answered 16/5, 2012 at 17:27 Comment(0)
R
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

Residual answered 2/9, 2022 at 8:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.