我有一個問題,在WPF中的按鈕。
我正在開發一個有很多頁面和相關按鈕(> 50)的GUI。
圖形用戶界面應該與觸摸屏顯示器一起使用,所以每個按鈕只有兩個圖形狀態按下和正常(沒有懸停狀態),就像智能手機應用程序中的任何按鈕一樣。
目前,我使用的這個解決方案在這裏:C#WPF - 兩個狀態按鈕問題。 (觸摸屏點擊事件)
資源文件
<Application.Resources>
<Style x:Key="flatButtonStyle" TargetType="Button">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="border"
Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Left" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
在頁面的一個
<Button
Uid=".../image_up.png"
Tag=".../image_down.png"
Style="{StaticResource flatButtonStyle}"
TouchLeave="buttonImageUp"
TouchDown="buttonImageDown"
Click="clickEvent"
ClickMode="Press"
>
<Button.Background>
<ImageBrush ImageSource=".../image_up.png" Stretch="Uniform"/>
</Button.Background>
</Button>
裏面的相關的C#文件
private void buttonImageDown(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
button.Content = new Image {
Source = new BitmapImage(new Uri(button.Tag + "")),
VerticalAlignment = VerticalAlignment.Center,
Stretch = Stretch.Uniform
};
}
private void buttonImageUp(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
button.Content = new Image {
Source = new BitmapImage(new Uri(button.Uid)),
VerticalAlignment = VerticalAlignment.Center,
Stretch = Stretch.Uniform
};
}
這實施有很多問題:
我不能使用UID和標籤
的響應速度慢,因爲它具有運行每個觸摸一個C#函數2時
我必須每次我需要時間將C#函數在新頁面中使用按鈕
但真正的問題是,我不知道爲什麼有時候clickEvent不會啓動時按下按鈕,並且在某些計算機上clickEvent僅適用於第3〜 5次比只touchDonw和touchLeave正在工作...
(我使用.NET 4.6.1但相同的行爲與4.5.2和4.7)
我認爲是能夠消除C#分量和改變按鈕的圖像(源極)只是與XAML,但我想了一會兒,我什麼也找不到。
有人可以幫我嗎?
你想模擬[ToggleButton](https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.togglebutton(v = vs.110).aspx)? – icebat
你不能用Style觸發器來設置圖像嗎? Uid和Tag有什麼問題? –
@icebat是的,但使用C#並不是非常有效。 – xSanciopanzax