使用 WPF 样式来简化重复的多重绑定

发布于 2024-11-08 14:32:36 字数 2854 浏览 3 评论 0原文

我正在显示多项测试的结果。每个测试有 2 个变量,显示结果的文本和颜色因两者而异。我已经完成了这个工作,但是有很多重复的 MultiBinding 正在进行,我想看看是否有一种方法可以使用 Style 来简化 XAML。

下面是我的代码的简化外观:

//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 -->

我想定义一个样式,以便我可以更轻松地将 MultiBindings 应用于 TextBlocks。每个测试的唯一区别是我在绑定路径值中指定的测试。我的目标是这样的:

<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 --> />

有类似的事情可能吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

站稳脚跟 2024-11-15 14:32:36

您可以执行以下操作:

<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}" />

在这里,您传入“Test”作为默认绑定上下文,该上下文由 Style 使用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文