Use a WPF Style to simplify repetitive MultiBindings

jwismar picture jwismar · May 20, 2011 · Viewed 7.7k times · Source

I am displaying the results of several tests. There are 2 variables on each test, and the text and color of the displayed results vary with both. I have this working, but there's a lot of repetitive MultiBinding going on, and I'd like to see if there's a way to use a Style to simplify the XAML.

Here's a simplified look at what my code looks like:

//TestResults.cs excerpt
public class TestResults
{
    private Test1Result test1 = new Test1Result();
    public Test1Result Test1 { get { return test1; } }

    private Test2Result test2 = new Test2Result();
    public Test2Result Test2 { get { return test2; } }
}

//TestCtrl.xaml.cs excerpt
public class TestCtrl : UserControl
{
    private TestResults results = new TestResults();
    public TestResults Results { get { return results; } }
}
<!-- TestCtrl.xaml excerpt -->
<UserControl x:Class="Tester.TestCtrl" x:Name="TestResultsCtrl" ...>

<!-- lots of stuff -->

<TextBlock Grid.Row="6">
    <TextBlock.Text>
        <MultiBinding Converter="{StaticResource TestToTextConverter}" >
            <Binding Path="Results.Test1.Temperature" ElementName="TestResultsCtrl" />
            <Binding Path="Results.Test1.Time" ElementName="TestResultsCtrl" />
        </MultiBinding>
    </TextBlock.Text>
    <TextBlock.Foreground>
        <MultiBinding Converter="{StaticResource TestToBrushConverter}">
            <Binding Path="Results.Test1.Temperature" ElementName="TestResultsCtrl" />
            <Binding Path="Results.Test1.Time" ElementName="TestResultsCtrl" />
        </MultiBinding>
    </TextBlock.Foreground>
</TextBlock>

<TextBlock Grid.Row="7">
    <TextBlock.Text>
        <MultiBinding Converter="{StaticResource TestToTextConverter}" >
            <Binding Path="Results.Test2.Temperature" ElementName="TestResultsCtrl" />
            <Binding Path="Results.Test2.Time" ElementName="TestResultsCtrl" />
        </MultiBinding>
    </TextBlock.Text>
    <TextBlock.Foreground>
        <MultiBinding Converter="{StaticResource TestToBrushConverter}">
            <Binding Path="Results.Test2.Temperature" ElementName="TestResultsCtrl" />
            <Binding Path="Results.Test2.Time" ElementName="TestResultsCtrl" />
        </MultiBinding>
    </TextBlock.Foreground>
</TextBlock>

<!-- lots more repetitions here -->

I would like to define a Style so that I can apply the MultiBindings to the TextBlocks more easily. The only difference in each one is which test I specify in the Binding Path values. My goal would be something like:

<Style x:Key="TestResultsStyle" TargetType="{x:Type TextBlock}">
    <!-- do binding stuff here -->
</Style>

<TextBlock Grid.Row="6" 
    Style="{StaticResource TestResultsStyle}" <!--set Test1 path here --> />
<TextBlock Grid.Row="7" 
    Style="{StaticResource TestResultsStyle}" <!--set Test2 path here --> />

Is anything similar to this possible?

Answer

CodeNaked picture CodeNaked · May 21, 2011

You could do something like this:

<Style x:Key="TestResultsStyle" TargetType="{x:Type TextBlock}">
    <Setter Property="Text">
        <Setter.Value>
            <MultiBinding Converter="{StaticResource TestToTextConverter}" >
                <Binding Path="Temperature" />
                <Binding Path="Time" />
            </MultiBinding>
        </Setter.Value>
    </Setter>
    <!-- Same for Foreground -->
</Style>

<TextBlock Grid.Row="6" 
    DataContext="{Binding ElementName=TestResultsCtrl, Path=Results.Test1}"
    Style="{StaticResource TestResultsStyle}" />
<TextBlock Grid.Row="7" 
    DataContext="{Binding ElementName=TestResultsCtrl, Path=Results.Test2}"
    Style="{StaticResource TestResultsStyle}" />

Here you pass in the "Test" as the default binding context, which is used by the Style.