2013-02-07 34 views
0

我有MenuItem列表和使用ApplicationCommnds喜歡剪切,複製,粘貼。我想做一些事情時,Command是disable.But樣式不工作它。ApplicationCommand默認行爲是ii自動設置如果它禁用,前景灰色。但它不適用於我的情況。所以我明確地嘗試設置它。風格不適用於應用程序命令

<TextBox x:Name="AssignmentTextBox" > 
    <TextBox.ContextMenu> 
     <ContextMenu Background="White"> 
      <MenuItem Command="ApplicationCommands.Undo" Style="StaticResource _MenuItem}"/>         
      <Separator /> 
      <MenuItem Command="ApplicationCommands.Cut" Style="{StaticResource _MenuItem}"/> 
      <MenuItem Command="ApplicationCommands.Copy" Style="{StaticResource _MenuItem}" /> 
      <MenuItem Command="ApplicationCommands.Paste" Style="{StaticResource _MenuItem}"/>          
      <Separator /> 
      <MenuItem Command="ApplicationCommands.SelectAll" Style="{StaticResource _MenuItem}"/> 
     </ContextMenu> 
    </TextBox.ContextMenu> 
</TextBox> 
+0

樣式聲明是什麼樣的? – OmegaMan

+0

哪裏定義了_MenuItem風格?在應用程序資源?或某處? – Somnath

+0

你想做什麼「某件事」? menutiem或其他項目的風格變化,代碼觸發器,什麼樣的? – iltzortz

回答

1

明白了。您在設置第一個MenuItem的Style屬性值時錯過了支架{

什麼是錯的

<MenuItem Command="ApplicationCommands.Undo" Style="StaticResource _MenuItem}"/> 

什麼是正確的

<MenuItem Command="ApplicationCommands.Undo" Style="{StaticResource _MenuItem}"/> 

[編輯] 在測試情況下啓用的MenuItems將是綠色和殘疾人的MenuItems將是紅色。希望這將幫助你解決你的問題

<ContextMenu Background="White"> 
     <ContextMenu.Resources> 
      <Style x:Key="_MenuItem1" TargetType="{x:Type MenuItem}"> 
        <Style.Triggers> 
          <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsEnabled}" Value="false"> 
           <Setter Property="Foreground" Value="Red"/> 
          </DataTrigger> 
          <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsEnabled}" Value="True"> 
           <Setter Property="Foreground" Value="Green"/> 
          </DataTrigger> 
        </Style.Triggers> 
      </Style> 
     </ContextMenu.Resources> 
     <MenuItem Command="ApplicationCommands.Undo" Style="{StaticResource _MenuItem1}"/> 
     <Separator /> 
     <MenuItem Command="ApplicationCommands.Cut" Style="{StaticResource _MenuItem1}"/> 
     <MenuItem Command="ApplicationCommands.Copy" Style="{StaticResource _MenuItem1}" /> 
     <MenuItem Command="ApplicationCommands.Paste" Style="{StaticResource _MenuItem1}"/> 
     <Separator /> 
     <MenuItem Command="ApplicationCommands.SelectAll" Style="{StaticResource _MenuItem1}"/> 
    </ContextMenu> 

截圖

enter image description here

+0

Somnath - 根據代碼所有MenuItem Forground應該是紅色的,但它仍然是黑色的。那是我的問題。 – Nitin

+0

@ user1939423我已根據您的需要更新了答案。讓我們認爲啓用的MenuItems將是綠色的並且禁用MenuItems將是紅色的。我已經更新了答案。您也可以按照「iltzortz」提供的示例進行操作。希望這會幫助你解決你的問題。 – Somnath

0

看來你已經改變了菜單項模板或覆蓋的前景,使得CommandCan執行不能更新正確的視覺元素呈現爲「禁用」,應該是灰色。

所以我不知道你是否可以用鍵_MenuItem向我們展示模板或你的風格,也許我們可以告訴你的情況有什麼問題。