2012-02-08 72 views
2

有沒有辦法在應用程序級別爲控件類型設置默認光標?我想說,所有的Button控件,無論它們是否具有特定的樣式,都有一個手形光標的默認光標,除非在該按鈕的個別樣式規範中被覆蓋。全局按鈕光標

這裏是有自己的風格,例如一個按鈕,我想覆蓋默認

<UserControl> 
    <UserControl.Resources> 
     <Style x:Key="CloseButtonStyle" TargetType="{x:Type Button}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type Button}"> 
         <Grid> 
          <!-- My button's custom content here --> 
         </Grid> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </UserControl.Resources> 

    <Button x:Name="btnClose" Style="{DynamicResource CloseButtonStyle}"/> 
</UserControl> 

回答

9

把風格低於Application.Resources在App.xaml文件的一個例子。

<Style TargetType="Button"> 
    <Setter Property="Cursor" Value="Hand"/> 
</Style> 

UPDATE

在關於第三評論: 要做到這一點,你需要離開只是你的控制模板UserControl.Resources

<ControlTemplate x:Key="CloseButtonTemplate" TargetType="{x:Type Button}"> 
    <Grid> 
     <!-- My button's custom content here --> 
    </Grid> 
</ControlTemplate> 

然後設置Template屬性Button

<Button Template="{DynamicResource CloseButtonTemplate}"/> 
+0

這似乎不適用於我的按鈕。我用一個按鈕的例子編輯了我的問題,我添加了一個自定義樣式,我希望繼承遊標值,以便能夠提供解決方案的洞察。 – HotN 2012-02-08 18:38:44

+0

如果它不起作用,您可能需要檢查您的解決方案的默認按鈕的樣式(如在答案中)。我的意思是,當您創建不帶x:Key的樣式時,只需指定類型 - 它將作爲其定義範圍的默認樣式。我在檢查臨時項目後發佈了這個答案。如果你創建臨時解決方案,你可以自己檢查一下,在MainWindow兩個按鈕中輸入你的App.xaml中的答案並指定樣式。 – 2012-02-08 20:21:44

+0

您是否介意在評論中發佈臨時解決方案的MainWindow代碼?按照您的建議,我使用溫度2按鈕解決方案進行了試用。一個標準按鈕,直接來自工具箱,與你的例子非常相稱。在樣式中指定了一個模板的按鈕(如上面所示),在控件模板中使用矩形的簡單示例時,仍然顯示無手形光標。 – HotN 2012-02-08 23:13:23