Button style that can present multiple text strings?
I need to have a button style that will present 3 different texts of the binded stings. Now, I can easily pass one text string to Content Presenter where Content="{TemplateBinding Content}". I need to be able to pass 3 strings in a similar way to 3 either texblock inside button style or use multiple Content presenters. I am wond开发者_运维技巧ering how I can do this. Any ideas are highly appreciated?
You can use a custom DataTemplate:
<Button>
<Button.ContentTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="1" />
<TextBlock Text="2" />
<TextBlock Text="3" />
</StackPanel>
</DataTemplate>
</Button.ContentTemplate>
</Button>
You can swap out or change the layout by altering the StackPanel.
If it needs to be a Style, you could use:
<Style TargetType="Button">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding One}" />
<TextBlock Text="{Binding Two}" />
<TextBlock Text="{Binding Three}" />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
And then you'd need to define a custom class to pass the three values like so:
public class MyClass {
public string One { get; set; }
public string Two { get; set; }
public string Three { get; set; }
}
And then use it like so:
<Button>
<local:MyClass One="1" Two="2" Three="3" />
</Button>
Where local refers to the namespace of MyClass.
精彩评论