WPF之XAML積累

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;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章