0
我最近一直在玩WPF和我遇到了一些我無法解決的問題。我的generic.xaml中有以下代碼:WPF綁定和轉換器
<Style TargetType="{x:Type local:ClearButton}">
<Style.Resources>
<con:ValueConverter x:Key="converter" />
</Style.Resources>
<Setter Property="Width" Value="20" />
<Setter Property="Height" Value="20" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ClearButton}">
<Grid>
<Image Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="/WPF-Libraries;component/Resources/ClearEnabled.png" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BitmapEffect">
<Setter.Value>
<OuterGlowBitmapEffect Opacity="0.5" GlowColor="Red" GlowSize="3" />
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Source"
Value="/WPF-Libraries;component/Resources/ClearDisabled.png" />
</Trigger>
<!--Binding #1-->
<Trigger Property="{TemplateBinding local:ClearButton.IsPressed}" Value="True">
<Setter Property="RenderTransform">
<Setter.Value>
<!--Binding #2-->
<ScaleTransform CenterX="CONVERTER BINDING:PASS WIDTH TO CONVERTER" CenterY="CONVERTER BINDING:PASS HEIGHT TO CONVERTER" ScaleX="0.75" ScaleY="0.75" />
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我不能讓綁定#1工作。我想將觸發器綁定到按鈕的IsPressed屬性,綁定應該是什麼?如果我想將按鈕的寬度和高度傳遞給轉換器,Binding#2應該綁定什麼?
我也可以設置觸發這種方式來代替:
<Style TargetType="{x:Type local:ClearButton}">
<Style.Resources>
<con:ValueConverter x:Key="converter" />
</Style.Resources>
<Setter Property="Width" Value="20" />
<Setter Property="Height" Value="20" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ClearButton}">
<!--Abbreviated-->
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<!--Binding #1-->
<Trigger Property="{TemplateBinding local:ClearButton.IsPressed}" Value="True">
<Setter Property="RenderTransform">
<Setter.Value>
<!--Binding #2-->
<ScaleTransform CenterX="CONVERTER BINDING" CenterY="CONVERTER BINDING" ScaleX="0.75" ScaleY="0.75" />
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
哪一個更好,會是什麼綁定是綁定#1和#2?
觸發器的Property屬性? – Austin 2009-12-27 21:53:27
對不起,我遇到了另一個問題。嘗試你的方法後,它現在檢測用戶何時按下按鈕,但由於某種原因,轉換器不工作。這裏是代碼: http://snippets.symfony-project.org/snippet/381 CenterX和CenterY似乎等於0.我想這是一個綁定的問題。 – Austin 2009-12-27 22:22:11
當您在未指定源的情況下使用綁定時,默認值是包含綁定的對象的DataContext。您可能希望參考按鈕的寬度,但該按鈕不是它自己的數據上下文。爲了能夠引用按鈕對象,請將以下內容添加到您的綁定中:'RelativeSource = {RelativeSource Mode = FindAncestor,AncestorType = Button}' – 2009-12-28 09:32:42