SumGuy
SumGuy

Reputation: 491

WPF Issues with binding to styled button

I have created a RadioButton style which I use across my application. The display part of which uses the content presenter to display whatever content I added to the button:

<ContentPresenter>
   <ContentPresenter.ContentTemplate>
      <DataTemplate>
         <Grid>
            <TextBlock Text="{TemplateBinding Content}" />
         </Grid>
      </DataTemplate>
   </ContentPresenter.ContentTemplate>
</ContentPresenter>

I'm then attempting to bind a decimal with a string formatter to the styled button like so:

<RadioButton Content="{Binding Stake, StringFormat={}{0:C}}" Style="{DynamicResource NeutralSelectorButtonStyle}" />

Stake is a decimal within a ViewModel which is set as the DataContext. When I run this up the content coming through is blank.

I made a change using a label in the DataTemplate rather than a TextBlock, this displayed the decimal but had not formatted it.

Can anyone explain why this is happening and possibly provide a solution.

If you require any more information just ask :)

Thanks in advance.

Upvotes: 0

Views: 86

Answers (1)

dev hedgehog
dev hedgehog

Reputation: 8791

You are almost there just instead of setting the string format inside binding you should use ContentStringFormat property when in ContentControls.

Take a look at this Label (it works with any content control):

<Label Content="{Binding Path=MaxLevelofInvestment}" ContentStringFormat="Amount is {0}"/>

ContentPresenter also has this property:

http://msdn.microsoft.com/en-us/library/system.windows.controls.contentpresenter.contentstringformat%28v=vs.110%29.aspx

Try it out. I hope it works for you.

Upvotes: 1

Related Questions