使用 WPF 样式来简化重复的多重绑定
我正在显示多项测试的结果。每个测试有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以执行以下操作:
在这里,您传入“Test”作为默认绑定上下文,该上下文由 Style 使用。
You could do something like this:
Here you pass in the "Test" as the default binding context, which is used by the Style.