[英]WPF: Change ListBoxItem Background property for MouseOver programmatically
[英]Change button background WPF mouseover property with c# conditions
我有一個很大的問題,我不知道該怎么辦。 我在c#WPF上寫了一個客戶端。 我的表單上有3個元素(文本框,密碼框,按鈕)。 因此,當人們填寫文本框和密碼框文本時,我想將我的按鈕背景更改為“活動(更改圖像)”。 登錄和密碼之類的東西。
private void eventhandler(object sender, RoutedEventArgs e)
{
if (login.Text.Length > 0 && password.Password.Length > 0)
{
ImageBrush myBrush = new ImageBrush();
myBrush.ImageSource =
new BitmapImage(new Uri("pack://application:,,,/Images/Enter Enabled.bmp", UriKind.Absolute));
Enter.Background = myBrush;
}
else if (login.Text.Length < 1 || password.Password.Length < 1)
{
ImageBrush myBrush = new ImageBrush();
myBrush.ImageSource =
new BitmapImage(new Uri("pack://application:,,,/Images/Enter Disabled.bmp", UriKind.Absolute));
Enter.Background = myBrush;
}
}
所以我有這個c#代碼。 它有效,但現在我有一個問題。 當我將鼠標懸停在按鈕上時它會將其背景更改為“已禁用”。
XAML
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="250" Width="288" Background="#FF494949">
<Window.Resources>
<Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
<Setter Property="FocusVisualStyle">
<Setter.Value>
<Style>
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="2" SnapsToDevicePixels="True" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
<Setter Property="Background" Value="#FFDDDDDD"/>
<Setter Property="BorderBrush" Value="#FF707070"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsDefaulted" Value="True">
<Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="border">
<Setter.Value>
<ImageBrush ImageSource="Images/Enter Disabled.bmp"/>
</Setter.Value>
</Setter>
<Setter Property="BorderBrush" TargetName="border" Value="#FF3C7FB1"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" TargetName="border">
<Setter.Value>
<ImageBrush ImageSource="Images/Enter Pressed.bmp"/>
</Setter.Value>
</Setter>
<Setter Property="BorderBrush" TargetName="border" Value="#FF2C628B"/>
</Trigger>
<Trigger Property="ToggleButton.IsChecked" Value="True">
<Setter Property="Background" TargetName="border" Value="#FFBCDDEE"/>
<Setter Property="BorderBrush" TargetName="border" Value="#FF245A83"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" TargetName="border" Value="#FFF4F4F4"/>
<Setter Property="BorderBrush" TargetName="border" Value="#FFADB2B5"/>
<Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="#FF838383"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid Margin="0,0,2,-4.478">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0*"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button HorizontalAlignment="Left" Name="Enter" Margin="46,92,0,0" VerticalAlignment="Top" Width="127" Height="27" Grid.Column="1" BorderThickness="0" Style="{DynamicResource ButtonStyle1}">
<Button.Background>
<ImageBrush ImageSource="Images/Enter Disabled.bmp"/>
</Button.Background>
</Button>
<TextBox Grid.ColumnSpan="2" Name="login" HorizontalAlignment="Left" Height="23" Margin="53,23,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" TextChanged="eventhandler"/>
<PasswordBox Grid.ColumnSpan="2" Name="password" HorizontalAlignment="Left" Margin="53,59,0,0" VerticalAlignment="Top" Width="120" PasswordChanged="eventhandler"/>
</Grid>
我知道這是因為Mouseover圖像畫筆,但我如何鎖定它或改變c#代碼與上述條件。 Testapplication
我已經在takebin上傳了testapp。 所以只需用文本和鼠標懸停按鈕填充文本框和密碼框,您就會看到問題所在。 謝謝大家的回復和任何幫助
同意。 如果您不想要這樣的行為,請刪除它:
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="border">
<Setter.Value>
<ImageBrush ImageSource="Images/Enter Disabled.bmp"/>
</Setter.Value>
</Setter>
<Setter Property="BorderBrush" TargetName="border" Value="#FF3C7FB1"/>
</Trigger>
WPF按鈕已包含此功能。 您可以將按鈕的IsEnabled屬性綁定到視圖模型中的bool值,然后使用可視狀態在樣式xml中定義啟用,禁用和moseover的樣式。
您可以在此處找到默認WPF按鈕樣式的源代碼: https : //msdn.microsoft.com/en-us/library/ms753328(v = vs.110).aspx
然后,您還可以根據文本框中的屬性更改更改綁定到IsEnabled的屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.