2013-08-31 27 views
7

Tôi đã tạo điều khiển người dùng sau. . Khi tôi thêm nó vào một cửa sổ XAML, tôi nhận được báo lỗi "Không thể tạo một thể hiện của 'ucAppItem' Tôi kéo điều khiển người dùng vào cửa sổ từ thanh công cụKhông thể tạo trường hợp lỗi "[kiểm soát người dùng]" trong thiết kế

XAML của điều khiển người dùng được như sau:.

<UserControl x:Class="Demos.ucAppItem" 
      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" Width="852" Height="215"> 
    <Grid> 

     <Label Name="lblTitle" Content="Title" HorizontalAlignment="Left" Margin="233,10,0,0" VerticalAlignment="Top" FontSize="22" FontFamily="Arial"/> 


     <Image Width="40" Height="40" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,0,80,0"> 
      <Image.Style> 
       <Style TargetType="{x:Type Image}"> 
        <Setter Property="Source" Value="pack://siteoforigin:,,,/arrow2.png"/> 
        <Style.Triggers> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter Property="Source" Value="pack://siteoforigin:,,,/arrow1.png"/> 
         </Trigger> 
        </Style.Triggers> 
       </Style> 
      </Image.Style> 
     </Image> 
     <Label x:Name="lblRun" Content="Run" HorizontalAlignment="Right" Margin="0,88,35,0" VerticalAlignment="Top" Foreground="#FF2EAADC" FontSize="20"> 
      <Label.Style> 
       <Style TargetType="{x:Type Label}"> 
        <Setter Property="Foreground" Value="#FF2EAADC"/> 
        <Style.Triggers> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter Property="Foreground" Value="#006d9e"/> 
         </Trigger> 
        </Style.Triggers> 
       </Style> 
      </Label.Style> 
     </Label> 
    </Grid> 
</UserControl> 

XAML của cửa sổ như sau:

<Window 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:Demos" x:Class="Demos.Window1" 
     Title="Window1" Height="487" Width="854"> 
    <Grid> 

     <local:ucAppItem/> 

    </Grid> 
</Window> 

Nhờ sự giúp đỡ của bạn trước

+0

làm việc cho tôi. Bạn có bất cứ điều gì được viết bằng mã phía sau điều khiển người dùng không? – Nitin

Trả lời

4

@Anatoily Nikolaev - cảm ơn sự giúp đỡ của bạn! Con trỏ của bạn trên nhãn đã khắc phục vấn đề tôi đã gặp phải và bạn đã đúng về hình ảnh. Tôi sẽ đánh dấu câu trả lời của bạn là câu trả lời. Đó là nguồn gốc là vấn đề.

nhãn của tôi bây giờ được định nghĩa là:

<Label x:Name="lblRun" Content="Run" HorizontalAlignment="Right" Margin="0,88,35,0" VerticalAlignment="Top" FontSize="20"> 
     <Label.Style> 
      <Style TargetType="{x:Type Label}"> 
       <Setter Property="Foreground" Value="#FF2EAADC"/> 
       <Style.Triggers> 
        <Trigger Property="IsMouseOver" Value="True"> 
         <Setter Property="Foreground" Value="#006d9e"/> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </Label.Style> 
    </Label> 

hình ảnh của tôi bây giờ được định nghĩa là:

 <Image> 
      <Image.Style> 
       <Style TargetType="{x:Type Image}"> 
        <Setter Property="Source" Value="{StaticResource arrow2}"/> 
        <Setter Property="Height" Value="40"/> 
        <Setter Property="Width" Value="40"/> 
        <Setter Property="VerticalAlignment" Value="Center"/> 
        <Setter Property="Margin" Value="0,0,80,0"/> 
        <Setter Property="HorizontalAlignment" Value="Right"/> 
        <Style.Triggers> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter Property="Source" Value="{StaticResource arrow1}"/> 
         </Trigger> 
        </Style.Triggers> 
       </Style> 
      </Image.Style> 
     </Image> 

Và tôi có nguồn lực (thiết lập trong file App.xaml), thiết lập như thế này:

<Application x:Class="demos.App" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      StartupUri="MainWindow.xaml"> 
    <Application.Resources> 
     <BitmapImage x:Key="arrow1" UriSource="arrow1.png" /> 
     <BitmapImage x:Key="arrow2" UriSource="arrow2.png" /> 
    </Application.Resources> 
</Application> 
+0

Tôi đã gặp sự cố tương tự với một số nút bằng ImageBrushes. Di chuyển các tài nguyên bàn chải ra khỏi UserControl.Resources và vào Application.Resources như trong ví dụ của bạn hóa ra là phần quan trọng tôi đã mất tích. – jmbpiano

3

Thứ nhất, thay vì pack://siteoforigin:,,,/arrow2.png cần phải viết thực tế của bạn 01.235.và để đảm bảo rằng các tập tin tồn tại trong dự án như một nguồn lực, như thế này (MSDN):

pack://application:,,,/arrow1.png 

Thứ hai, kích hoạt phong cách cho nhãn lblRun, sẽ không làm việc vì bạn thiết lập giá trị Foreground này tại địa phương, trong WPF có một danh sách các giá trị ưu tiên (MSDN), rằng giá trị địa phương của một ưu tiên cao hơn kích hoạt các phong cách:

<Label x:Name="lblRun" Foreground="#FF2EAADC" FontSize="20" ... /> 

Cố gắng loại bỏ nó Foreground giá trị địa phương và sử dụng Style setter:

<Label x:Name="lblRun" Content="Run" HorizontalAlignment="Right" Margin="0,88,35,0" VerticalAlignment="Top" FontSize="20"> 
    <Label.Style> 
     <Style TargetType="{x:Type Label}"> 
      <Setter Property="Foreground" Value="#FF2EAADC"/> 

      <Style.Triggers> 
       <Trigger Property="IsMouseOver" Value="True"> 
        <Setter Property="Foreground" Value="#006d9e"/> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Label.Style> 
</Label> 
+0

Cảm ơn rất nhiều vì những gợi ý đó! Tôi đã thực hiện những thay đổi đó nhưng tiếc là nó vẫn cho lỗi thiết kế! –

+0

@Rob McCabe: Thật kỳ lạ, đối với tôi nó hoạt động. Cùng một loại lỗi? Nếu bạn nhận xét điều khiển 'Hình ảnh', lỗi sẽ biến mất? –

+0

Hiện tại, tôi có thể thêm lại nhãn mà không có lỗi. Chỉ là hình ảnh của nó đang gây ra vấn đề. Đây là những gì tôi có: –

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