'u basitleştirmek için bir WPF Stili kullanın Birkaç testin sonuçlarını görüntülüyorum. Her testte 2 değişken vardır ve görüntülenen sonuçların metni ve rengi her ikisine de değişir. Bu çalışmam var, ama bir çok tekrarlayan MultiBinding var ve XAML'yi basitleştirmek için bir Stili kullanmanın bir yolu olup olmadığını görmek istiyorum.Yinelenen MultiBindings
İşte benim kod neye benzediğini de basitleştirilmiş bakalım:
//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 -->
ben daha kolay TextBlocks için MultiBindings uygulamak, böylece bir Stil tanımlamak istiyorum. Her birinde tek fark, Bağlama Yolu değerlerinde belirttiğim testtir. Hedefim şu gibi olabilir:
<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 --> />
Buna benzer bir şey var mı?
Bu mükemmel! Reklamı yapılan gibi çalışır. DataContext'e bakmam gerektiğine dair bir şüphem vardı, ama ekstra Bağlama ile nasıl bir araya getirileceğini anlayamadım. Teşekkürler!! – jwismar