我正在尝试学习WPF的一些内容,我对它的灵活性感到非常惊讶.
但是,我遇到了Styles和DataTemplates 的问题,这有点令人困惑.我已经定义了以下测试页,与周围风格等有点玩,发现Style在定义小号<Page.Resources>的Border,并TextBlock没有在应用DataTemplate,但Style对于ProgressBar以完全相同的方式定义应用.
源代码(我只使用Kaxaml和XamlPadX来查看结果)
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="Background" Value="SkyBlue"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="CornerRadius" Value="5"/>
</Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontWeight" Value="Bold"/>
</Style>
<Style TargetType="{x:Type ProgressBar}">
<Setter Property="Height" Value="10"/>
<Setter Property="Width" Value="100"/>
<Setter Property="Foreground" Value="Red"/>
</Style>
<XmlDataProvider x:Key="TestData" XPath="/TestData">
<x:XData>
<TestData xmlns="">
<TestElement>
<Name>Item 1</Name>
<Value>25</Value>
</TestElement>
<TestElement>
<Name>Item 2</Name>
<Value>50</Value>
</TestElement>
</TestData>
</x:XData> …Run Code Online (Sandbox Code Playgroud) 我希望得到你的意见,与无副作用的制定者有多远.
请考虑以下示例:
Activity activity;
activity.Start = "2010-01-01";
activity.Duration = "10 days"; // sets Finish property to "2010-01-10"
Run Code Online (Sandbox Code Playgroud)
请注意,日期和持续时间的值仅用于指示目的.
因此,使用设定器用于任何属性Start,Finish并且Duration将因此改变其他特性,因此不能被认为是侧无效.同样适用于实例Rectangle类,其中制定者X正在改变的价值观Top和Bottom等.
问题是你在哪里使用setter之间画一条线,它们具有改变逻辑相关属性值的副作用,以及使用方法,这些方法无论如何都不会更具描述性.例如,定义一个名为的方法SetDurationTo(Duration duration)也不会反映Start或Finish将被更改.