2010-10-27 32 views
5

Code:kích Disable phải và cho phép nhấp chuột trái để ContextMenu trong WPF sử dụng MVVM

<Button Style="{StaticResource HPForegroundStyle}" IsTabStop="False"     
     Command="{Binding ForegroundPhoneCommand}" Click="Button_Click"> 
        <Button.ContextMenu>     
         <ContextMenu ItemsSource="{Binding OptionsMenuItemList}"       ItemContainerStyle="{StaticResource ContextMenuItemStyle}" 
            IsOpen="{Binding IsMenuOpen}"           
            PlacementTarget="{Binding RelativeSourc={RelativeSource AncestorType={x:Type Button}}}"> 
         </ContextMenu> 
        </Button.ContextMenu> 
    </Button> 

tôi đang sử dụng MVVM pattern. Trong ViewModel tôi có một thuộc tính 'IsMenuOpen' điều khiển menu ngữ cảnh mở gần .Problem là tôi có thể vô hiệu hóa nhấp chuột phải và không thể hiển thị menu ngữ cảnh khi nhấp chuột trái.

+0

Bạn mở menu ngữ cảnh như thế nào? nó là một menu popup hoặc menu ngữ cảnh thực tế kiểm soát – TerrorAustralis

+0

một contextmenu của nó – suman

+0

và khi bạn nhấp chuột phải, nó không mở, nhưng nhấp chuột trái không mở nó? ForegroundPhoneCommand có làm cho thuộc tính IsMenuOpen được đặt thành true không? và thực hiện thuộc tính IsMenuOpen INotifyPropertyChange – TerrorAustralis

Trả lời

0

Nếu bạn muốn gắn trình đơn vào thuộc tính, hãy xem xét điều khiển Popup. Nó có chức năng tương tự như trình đơn ngữ cảnh nhưng không bị ràng buộc với một nút chuột cụ thể ...

<Popup IsVisible = {Binding IsMenuOpen} > 
    <!-- Fill in what you need here --> 
</Popup> 
2

Điều này đã giúp tôi sử dụng XAML tương tự như câu hỏi.

private bool _isMenuOpen = false; 
public bool IsMenuOpen 
{ 
    get { return _isMenuOpen; } 
    set 
    { 
     // Don't allow the UI (right-click) to set this property to true 
     if (!value) 
      _isMenuOpen = value; 
    } 
} 

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    Button btn = sender as Button; 
    _isMenuOpen = true; 
    btn.ContextMenu.IsOpen = true; 
} 
0

Bạn có thể làm điều đó chỉ như thế:

<Button x:Name="btn" Click="btn_Click" MouseRightButtonDown="btn_MouseRightButtonDown"> 
    <Button.ContextMenu> 
     <ContextMenu x:Name="popup" Visibility="Collapsed"> 
      <MenuItem Header="aaa"></MenuItem> 
      <MenuItem Header="bbb"></MenuItem> 
     </ContextMenu> 
    </Button.ContextMenu> 
</Button> 

private void btn_Click(object sender, RoutedEventArgs e) 
{ 
    popup.Visibility = Visibility.Visible; 
    popup.IsOpen = true; 
} 

private void btn_MouseRightButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    popup.Visibility = Visibility.Collapsed; 
} 
2

Một số điều cần theo dõi:

  1. Đảm bảo rằng DataContext của ContextMenu là hợp lệ.
  2. Đảm bảo rằng IsOpen là một ràng buộc hai chiều.
  3. Nếu bạn định thử mở nhấp chuột trái, hãy nhớ rằng Mục tiêu vị trí không hợp lệ, vì vậy bạn sẽ phải đặt Button.ContextMenu.PlacementTarget = this, và sau đó IsMenuOpen = true để hiển thị.

đoạn mã của tôi để tham khảo:

<Style x:Key="SubjectButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource CommandButtonStyle}"> 
<Setter Property="Foreground" Value="Green" /> 
<Setter Property="ContextMenu"> 
    <Setter.Value> 
     <ContextMenu DataContext="{Binding PlacementTarget.DataContext.Manager, RelativeSource={RelativeSource Self}}" 
        ItemsSource="{Binding SubjectManager.ContextMenuItems}" 
        IsOpen="{Binding SubjectManager.ContextMenuIsOpen, Mode=TwoWay}"> 
      <ContextMenu.ItemContainerStyle> 
       <Style TargetType="MenuItem"> 
        <Setter Property="Command" Value="{Binding OnClick}" /> 
       </Style> 
      </ContextMenu.ItemContainerStyle> 
     </ContextMenu> 
    </Setter.Value> 
</Setter> 
<Style.Triggers> 
    <Trigger Property="IsMouseOver" Value="True"> 
     <Setter Property="Foreground" Value="DarkGreen" /> 
    </Trigger> 
</Style.Triggers> 
</Style> 

Và trong mô hình điểm:

public void ShowContextMenu(SearchCondition searchCondition, Button button) 
{ 
    button.ContextMenu.DataContext = this; 
    SubjectManager.OpenContextMenu(); 
} 
18

Bạn có thể kiểm tra cũng ContextMenuService.IsEnabled tài sản trên các điều khiển phụ huynh đi kèm. Nó sẽ chỉ chặn nhấp chuột phải và bạn vẫn có thể hiển thị menu theo cách thủ công trên nhấp chuột trái, do đó dựa trên ví dụ trước:

<Button x:Name="btn" Click="btn_Click" ContextMenuService.IsEnabled="false"> 
    <Button.ContextMenu> 
     <ContextMenu x:Name="popup"> 
     ... 
     </ContextMenu> 
    </Button.ContextMenu> 
</Button> 

private void btn_Click(object sender, RoutedEventArgs e) 
{ 
    popup.Visibility = Visibility.Visible; 
    popup.IsOpen = true; 
} 
+1

Nghiêm túc điều này rõ ràng không hoạt động chút nào .. menu ngữ cảnh của tôi vẫn xuất hiện và biến mất ngay lập tức – user1034912

Các vấn đề liên quan