Button阴影边框
<Button.Effect>
<DropShadowEffect Color="Gray" Opacity=".50" ShadowDepth="8" />
</Button.Effect>
StringFormat
根据txt1的Text值,显示showXXX
<TextBox x:Name="txb1" Height="40" Width="200" Margin="160,24,157,256"/>
<TextBlock x:Name="txt1" Height="40" Text="{Binding ElementName=txt1,Path=Text,StringFormat='Show{0}'}" Margin="160,86,157,194">
根据txt1的Tag值,显示+XXX/-XXX/0(Tag必须是int类型,正数显示+,负数显示-,0显示0)
<TextBlock x:Name="btnClick" Height="40" Text="{Binding ElementName=txt1,Path=Tag,StringFormat='+#;-#;0'}" Margin="160,86,157,194">
自行指定绑定的数据源RelativeSource
在数据绑定中,除了正常的数据模版绑定,还会涉及到模板内控件的事件绑定,在模板的数据绑定中只能显示绑定的XXXModel中的字段,因为DataContext类型为XXXModel。我们想绑定XXXViewModel中的CompleteCommand事件,就需要自行指定绑定的数据源。(其他属性如Content的绑定也同此)
AncestorLevel来确定向上查找的级别,AncestorType确定要查找的元素
<Window>
<Button Command="{Binding DataContext.XXXCommand, RelativeSource={RelativeSource AncestorType=Window}}" CommandParameter="{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1,AncestorType=ItemsPresenter}}" />
</Window>
System标签
根据Tag的值(此时Tag类型必须为Bool),使字体变颜色
.xaml
<Window xmlns:sys="clr-namespace:System;assembly=mscorlib">
<TextBlock x:Name="txt1" Height="40" Text="12345" Margin="160,86,157,194">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="Tag">
<Trigger.Value>
<sys:Boolean>true</sys:Boolean>
</Trigger.Value>
<Setter Property="Foreground" Value="Red"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Window>
.xaml.cs
txt1.Tag = true;