簡體   English   中英

具有可變圖像的簡單WPF按鈕控制模板?

[英]Simple WPF Button Control Template with variable images?

我對WPF還是陌生的(用它編寫我的第一個應用程序),但是基本上我想(或者認為我想要)創建一個具有3個圖像(空閑,懸停,onClick)的某種控件,但是我可以更改圖像。 所以現在我有:

                <Rectangle Height="29" Width="35" Margin="0,2,0,0"
                       HorizontalAlignment="Left" VerticalAlignment="Top" 
                       Name="BTN_OpenDeviceSettings" ToolTip="Open Device Settings"
                       Fill="{DynamicResource device_grid___open_grid}"
                       MouseEnter="BTN_OpenDeviceSettings_MouseEnter"
                       MouseLeave="BTN_OpenDeviceSettings_MouseLeave"
                       MouseLeftButtonDown="BTN_OpenDeviceSettings_MouseLeftButtonDown"
                       MouseLeftButtonUp="BTN_OpenDeviceSettings_MouseLeftButtonUp">
                </Rectangle>

而且效果很好。 但是我想將圖形與代碼分開,並且要使此工作有效,我在C#代碼中手動交換了填充圖像。 我看過類似的代碼:

<Button Name="btnNext" Padding="15,3" HorizontalAlignment="Right" Click="OnButtonClick">
    <Image Width="90" Height="90">
        <Image.Style>
            <Style TargetType="Image">
                <Setter Property="Source" Value="/resources/a.png" />
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Source" Value="/resources/b.png" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Image.Style>
    </Image>
</Button>

所以我認為這就是我想要的。 但理想情況下,我將擁有許多這些按鈕,因此我可以創建一些東西來做到這一點:

<MyButton>
  <Images idleImage="someimage.png" hoverImage="someOtherImage.png" clickImage="someOtherOtherImage.png" onCLick="some_cSharp_method_to_call" />
</MyButton>

第一種解決方案:您可以為按鈕創建控件模板(如果打算僅在按鈕內顯示圖像,則可以創建樣式),以聲明將用於重疊/單擊/其他操作的圖像。 該樣式將由所需的所有按鈕共享,因為它將具有指定的鍵。

第二種解決方案:您可以將按鈕子類化,並為懸停/空閑/單擊的圖像聲明3個依賴項屬性。 然后,對於子類按鈕,將需要使用該依賴項屬性的一些默認模板(如果按鈕僅顯示圖像,則為樣式)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM