我有一個 UserControl,當“IsEnabled”為假時,我需要更改它的外觀。我知道這是非常基本的 WPF 樣式,但我似乎無法將這些部分組合在一起。我假設我需要創建一個 Style,并為“Property="IsEnabled" Value="False" 添加一個觸發器。但是我在哪里放置 Style 定義?(<UserControl.Resources>?)我如何應用它? ...在 UserControl 或父視窗中?我的觸發器是否需要在 Setter 元素內?樣式是否需要應用于 UserControl 或其子級?我不知道我還需要問什么其他問題!
如果您覺得這是另一個問題的重復,請指導我。如果您知道一個很好的、簡單的 WPF 樣式教程可以回答我的問題,我將非常感激聽到它。
我的代碼如下所示:
<UserControl x:Class="UserControls.UCDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:UserControls"
mc:Ignorable="d"
d:DesignHeight="240" d:DesignWidth="200">
<UserControl.Resources>
</UserControl.Resources>
<Grid Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="5*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0" Background="{Binding BG}" >
<Ellipse Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5" Fill="DeepSkyBlue" />
</Grid>
<Grid Grid.Row="1" Background="Tomato" />
</Grid>
<uc:UCDemo x:Name="Demo" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="300,100,0,0" Width="80" Height="100" Style="{StaticResource uc:DemoStyle}"
Visibility="{Binding LightVisible, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Converter={StaticResource BooleanToVisibilityConverter}}" />
謝謝您的幫助!
uj5u.com熱心網友回復:
這取決于當IsEnabled為假時外觀應該如何變化。
可以將帶有 a 的簡單樣式Trigger直接分配給 UserControl 的Style屬性。下面的只是將 設定Opacity為 0.5。
<UserControl ...>
<UserControl.Style>
<Style TargetType="UserControl">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" Value="0.5"/>
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Style>
<Grid Background="Transparent">
...
</Grid>
</UserControl>
uj5u.com熱心網友回復:
我不確定我明白你的問題是什么。但是要改變IsEnabled一個控制元件的屬性,依賴于另一個控制元件是:假設它IsEnabled必須改變的控制元件是A,而A所依賴的控制元件是B。首先,為B選擇一個名稱: x:Name = "AccessCheckBox". 然后為控制元件 B 撰寫這樣的系結:
< ... IsEnabled = {Binding ElementName = "AccessCheckBox", Path="IsChecked".../>
完畢。我希望我明白你的意思。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/452447.html
