2013-04-17 19 views
6

Tôi có một Menu điều khiển WPF mà tôi thêm một số mục menu vào thời gian chạy. Nếu bạn muốn biết, lý do là chúng ta đọc dữ liệu từ một cơ sở dữ liệu và cần tạo các MenuItems dựa trên dữ liệu được trả về. Chúng tôi có những gì tôi muốn nói là một phong cách menu phức tạp (ít phức tạp nhất để đọc :)). Bây giờ, khi tôi thêm các mục trình đơn của tôi, tôi cứ bị lỗi này trong cửa sổ Debug của Visual Studio:"System.Windows.Data Error: 4" trong MenuItem động

System.Windows.Data Error: 4 : Cannot find source for binding with reference  'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment') 

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment') 

Hai lặp lại nhiều lần tôi đoán dựa trên số lượng MenuItems thêm. Các hành vi khác lẻ tôi đã nhận thấy là, sau khi các lỗi này xuất hiện trong cửa sổ gỡ lỗi, MenuControl thả xuống (mở) trên riêng của nó, chỉ bằng cách di chuyển chuột qua nó (không nhấp vào nó như bình thường thực hiện). Vì vậy, tôi đoán người ta phải làm với người kia.

Bất kỳ ý tưởng gì đang diễn ra ?? MenuControlCode khá dài nhưng nếu cần, tôi cũng có thể đăng nó.

Đây là mã XAML của MenuControl. Sau đó, trong đoạn code tôi chỉ sử dụng các "PrincipalMenu" và thêm MenuItems đến nó khi cần thiết:

<UserControl x:Class="EOGMapControl.MainPropertyGUI.MenuControl" 
     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" 
     mc:Ignorable="d" 
     d:DesignHeight="41" d:DesignWidth="380"> 
<UserControl.Resources> 
    <SolidColorBrush x:Key="HighlightedBackgroundBrush" Color="#FFDEDEDE" /> 
    <SolidColorBrush x:Key="MenuBackgroundBrush" Color="White" /> 
    <SolidColorBrush x:Key="NormalBorderBrush" Color="#FFE5DFDF" /> 
    <SolidColorBrush x:Key="SolidMenuFontBrush" Color="Black" /> 
    <SolidColorBrush x:Key="HighlightedText" Color="White" /> 
    <SolidColorBrush x:Key="menuItemBrush" Color="#FFB7B7B7" /> 
    <SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF" /> 
    <SolidColorBrush x:Key="SolidBorderBrush" Color="#888" /> 
    <Style x:Key="{x:Type Menu}" TargetType="{x:Type Menu}"> 
     <Setter Property="OverridesDefaultStyle" Value="True"/> 
     <Setter Property="SnapsToDevicePixels" Value="True"/> 
     <Setter Property="Height" Value="25"/> 
     <Setter Property="Foreground" Value="{StaticResource SolidMenuFontBrush}"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Menu}"> 
        <Border 
     Background="{StaticResource MenuBackgroundBrush}" 
     BorderBrush="{StaticResource MenuBackgroundBrush}" 
     BorderThickness="1"> 
         <StackPanel ClipToBounds="True" Orientation="Horizontal" IsItemsHost="True"/> 
        </Border> 

       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
    <Style x:Key="{x:Type MenuItem}" TargetType="{x:Type MenuItem}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type MenuItem}"> 
        <Border x:Name="Border" 
       Background="{TemplateBinding Background}" 
       BorderBrush="{TemplateBinding BorderBrush}" 
       BorderThickness="0"> 
         <Grid> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition x:Name="Col0" MinWidth="17" Width="Auto" SharedSizeGroup="MenuItemIconColumnGroup"/> 
           <ColumnDefinition Width="Auto" SharedSizeGroup="MenuTextColumnGroup"/> 
           <ColumnDefinition Width="Auto" SharedSizeGroup="MenuItemIGTColumnGroup"/> 
           <ColumnDefinition x:Name="Col3" Width="14"/> 
          </Grid.ColumnDefinitions> 

          <!-- ContentPresenter to show an Icon if needed --> 
          <ContentPresenter Grid.Column="0" Margin="4,0,6,0" x:Name="Icon" VerticalAlignment="Center" ContentSource="Icon"/> 

          <!-- Glyph is a checkmark if needed for a checkable menu --> 
          <Grid Grid.Column="0" Visibility="Hidden" Margin="4,0,6,0" x:Name="GlyphPanel" VerticalAlignment="Center"> 
           <Path x:Name="GlyphPanelpath" VerticalAlignment="Center" Fill="{TemplateBinding Foreground}" Data="M0,2 L0,4.8 L2.5,7.4 L7.1,2.8 L7.1,0 L2.5,4.6 z" FlowDirection="LeftToRight"/> 
          </Grid> 

          <!-- Content for the menu text etc --> 
          <ContentPresenter Grid.Column="1" 
          Margin="{TemplateBinding Padding}" 
          x:Name="HeaderHost" 
          RecognizesAccessKey="True" 
          ContentSource="Header"/> 

          <!-- Content for the menu IGT --> 
          <ContentPresenter Grid.Column="2" 
          Margin="8,1,8,1" 
          x:Name="IGTHost" 
          ContentSource="InputGestureText" 
          VerticalAlignment="Center"/> 

          <!-- Arrow drawn path which points to the next level of the menu --> 
          <Grid Grid.Column="3" Margin="4,0,6,0" x:Name="ArrowPanel" VerticalAlignment="Center"> 
           <Path x:Name="ArrowPanelPath" HorizontalAlignment="Right" VerticalAlignment="Center" Fill="{TemplateBinding Foreground}" Data="M0,0 L0,8 L4,4 z"/> 
          </Grid> 
          <!--IsOpen="{Binding Path=IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" 
          PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" 
          --> 
          <!-- The Popup is the body of the menu which expands down or across depending on the level of the item --> 
          <Popup Placement="Right" IsOpen="{TemplateBinding IsSubmenuOpen}" x:Name="SubMenuPopup" Focusable="false" PopupAnimation="Fade"> 
           <Border x:Name="SubMenuBorder" SnapsToDevicePixels="True" Background="{StaticResource WindowBackgroundBrush}" BorderBrush="{StaticResource SolidBorderBrush}" BorderThickness="1" Padding="2,2,2,2"> 
            <Grid x:Name="SubMenu" Grid.IsSharedSizeScope="True"> 
             <!--StackPanel holds children of the menu. This is set by IsItemsHost=True--> 
             <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle"/> 
             <!--<ScrollViewer CanContentScroll="True"> 
              <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" /> 
             </ScrollViewer>--> 
            </Grid> 
           </Border> 
          </Popup> 
         </Grid> 
        </Border> 

        <!-- These triggers re-configure the four arrangements of MenuItem to show different levels of menu via Role --> 
        <ControlTemplate.Triggers> 

         <!-- Role = TopLevelItem : this is a child menu item from the top level without any child items--> 
         <Trigger Property="Role" Value="TopLevelItem"> 
          <Setter Property="Padding" Value="6,1,6,1"/> 
          <Setter Property="Visibility" Value="Collapsed" TargetName="ArrowPanel"/> 
         </Trigger> 

         <!-- Role = SubMenuHeader : this is a child menu item which does not have children --> 
         <Trigger Property="Role" Value="SubmenuHeader"> 
          <Setter Property="DockPanel.Dock" Value="Top"/> 
          <Setter Property="Padding" Value="0,2,0,2"/> 
         </Trigger> 

         <!-- Role = SubMenuItem : this is a child menu item which has children--> 
         <Trigger Property="Role" Value="SubmenuItem"> 
          <Setter Property="DockPanel.Dock" Value="Top"/> 
          <Setter Property="Padding" Value="0,2,0,2"/> 
          <Setter Property="Visibility" Value="Collapsed" TargetName="ArrowPanel"/> 
         </Trigger> 

         <Trigger Property="IsSuspendingPopupAnimation" Value="true"> 
          <Setter Property="PopupAnimation" Value="None" TargetName="SubMenuPopup"/> 
         </Trigger> 

         <!-- If no Icon is present the we collapse the Icon Content --> 
         <Trigger Property="Icon" Value="{x:Null}"> 
          <Setter Property="Visibility" Value="Collapsed" TargetName="Icon"/> 
         </Trigger> 

         <!-- The GlyphPanel contains the CheckMark --> 
         <Trigger Property="IsChecked" Value="true"> 
          <Setter Property="Visibility" Value="Visible" TargetName="GlyphPanel"/> 
          <Setter Property="Visibility" Value="Collapsed" TargetName="Icon"/> 
         </Trigger> 

         <!-- Using the system colors for the Menu Highlight and IsEnabled--> 
         <Trigger Property="IsHighlighted" Value="true"> 
          <Setter Property="Background" Value="{StaticResource HighlightedBackgroundBrush}" TargetName="Border"/> 
          <Setter Property="Foreground" Value="{StaticResource HighlightedText}"/> 
          <Setter Property="BorderBrush" Value="{StaticResource NormalBorderBrush}" TargetName="Border"/> 
         </Trigger> 
         <Trigger Property="IsHighlighted" Value="false"> 
          <Setter Property="Background" Value="{StaticResource MenuBackgroundBrush}" TargetName="Border"/> 
          <Setter Property="Foreground" Value="{StaticResource SolidMenuFontBrush}"/> 
          <Setter Property="BorderBrush" Value="{StaticResource MenuBackgroundBrush}" TargetName="Border"/> 
         </Trigger> 
         <Trigger Property="IsEnabled" Value="false"> 
          <Setter Property="Foreground" Value="LightGray"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</UserControl.Resources> 
<UserControl.Foreground> 
    <SolidColorBrush /> 
</UserControl.Foreground> 
<Grid Height="24" Width="250" HorizontalAlignment="Left"> 
    <Grid.Background> 
     <SolidColorBrush /> 
    </Grid.Background> 
    <Menu Name="PrincipalMenu" Margin="0,0,-100,0"> 
    </Menu> 
</Grid> 

+0

Bạn không được tạo hoặc thao tác các phần tử giao diện người dùng trong mã trong WPF. Đăng mã của bạn và XAML. –

+0

Đây là dự án WPF đầu tiên của tôi nên không quen thuộc với WPF; nhưng tại sao vậy? Bạn có biết? –

+0

Đăng mã của bạn và XAML. Nếu không thì đó là tất cả những suy đoán. –

Trả lời

1

Vui lòng tham khảo liên kết dưới đây:

http://www.codeproject.com/Tips/124556/How-to-suppress-the-System-Windows-Data-Error-warn

Giải pháp là , thiết lập mức độ chuyển đổi nguồn ràng buộc dữ liệu là quan trọng trong hàm tạo của lớp.

public TestSample() //constructor of the class 
{ 
    InitializeComponent(); 

    System.Diagnostics.PresentationTraceSources.DataBindingSource.Switch.Level = System.Diagnostics.SourceLevels.Critical; 

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