我試圖讓一個由幾個按鈕和一對夫婦的標籤,自定義控制,但按鍵都應該是不可見的,只顯示在其中的內容。通過將它們設置爲透明,我可以擺脫邊框,背景等。但是,每當我將鼠標懸停在默認窗口懸停效果上時,再次顯示整個按鈕。我已經嘗試過很多關於自定義控件的指南,但最終無法弄清楚如何覆蓋它。基本上我的問題歸結爲,其中有多少可以被放置在generic.xaml文件,什麼組織我有該文件中使用,以及是否有任何其他地方,這些造型應該走呢?我意識到這是一個非常基本的問題,但這只是讓我無法弄清楚具體的答案。謝謝!自定義控件樣式/觸發器
電流XAML:
<Style TargetType="{x:Type local:TimePicker}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:TimePicker}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Height="{TemplateBinding Height}"
Width="{TemplateBinding Width}">
<StackPanel Orientation="Horizontal">
<StackPanel x:Name="PART_Root"
Orientation="Horizontal"
HorizontalAlignment="Center">
<ToggleButton x:Name="PART_HourButton"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="0"
Background="Transparent"
BorderBrush="Transparent"
BorderThickness="0"
Height="{Binding ElementName=PART_IncDecPanel, Path=ActualHeight}"
Width="{Binding ElementName=PART_HourButton, Path=ActualHeight}"
Content="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Hour}">
<ToggleButton.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Transparent" />
</Trigger>
</ToggleButton.Triggers>
</ToggleButton>
<Label x:Name="PART_HourMinSeparator"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="0"
Content=":" />
<ToggleButton x:Name="PART_MinButton"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="0"
Background="Transparent"
BorderBrush="Transparent"
BorderThickness="0"
Height="{Binding ElementName=PART_HourButton, Path=ActualHeight}"
Width="{Binding ElementName=PART_HourButton, Path=ActualWidth}"
Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Minute}" />
<StackPanel x:Name="PART_IncDecPanel" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button x:Name="PART_IncreaseTime"
Background="Transparent"
BorderBrush="Transparent"
BorderThickness="0"
HorizontalAlignment="Center"
HorizontalContentAlignment="Center"
VerticalAlignment="Center"
VerticalContentAlignment="Center"
Margin="0"
Padding="0"
Width="22"
Height="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IncreaseImage.ActualHeight}">
<Image Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IncreaseImage}" />
</Button>
<Button x:Name="PART_DecreaseTime"
Background="Transparent"
BorderBrush="Transparent"
BorderThickness="0"
HorizontalContentAlignment="Center"
HorizontalAlignment="Center"
VerticalAlignment="Center"
VerticalContentAlignment="Center"
Margin="0"
Padding="0"
Height="{Binding ElementName=PART_IncreaseTime, Path=ActualHeight}"
Width="{Binding ElementName=PART_IncreaseTime, Path=ActualWidth}">
<Image Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DecreaseImage}" />
</Button>
</StackPanel>
</StackPanel>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
如果你想按鈕,看上去並不像按鈕,當你將鼠標放在它們上面時,不會像按鈕一樣反應,那麼你確定你真的想要按鈕嗎?但對於你的問題,你將需要替換幾乎整個按鈕的默認模板。 – 2012-03-08 17:58:26
我是否在generic.xaml中執行此操作?我會在ToggleButton輪廓或其他地方做到這一點嗎? – 2012-03-08 18:28:14
非常符合您的用戶控制。爲你的ToggleButton設置一個樣式。用這種方式使用setter來設置模板。在模板中只需包含ContentPresenter(如flq建議),然後最終得到一個沒有自己展示的按鈕。請記住,如果您決定要保留模板的某些部分,但不是全部(可能在檢查時改變外觀),則必須在自己的模板中重新實現該模板。 – 2012-03-08 18:39:28