2011-03-04 43 views
2

我在将x:name的控件添加到自定义控件时遇到问题。Custom ItemsControl子控件命名空间问题

添加x:name =“startDate”会导致一切中断。否则,这完美的作品。

我有一个自定义控件继承自ItemsControl。

using System; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Ink; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 

namespace ControlCSI 
{ 
    public partial class SlideMenu : ItemsControl 
    { 
     public SlideMenu() 
     { 
      // Required to initialize variables 
      InitializeComponent(); 
     } 
    } 
} 

这里是一个类

<ItemsControl 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
x:Class="ControlCSI.SlideMenu" 
d:DesignWidth="200" d:DesignHeight="800"> 

<ItemsControl.ItemsPanel> 
    <ItemsPanelTemplate> 
     <StackPanel Orientation="Vertical" /> 
    </ItemsPanelTemplate> 
</ItemsControl.ItemsPanel> 

<ItemsControl.Template> 
    <ControlTemplate TargetType="ItemsControl"> 
     <Grid x:Name="LayoutRoot"> 
      <Border Height="auto" Grid.Column="0" CornerRadius="5" > 
       <Grid> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="23" /> 
         <ColumnDefinition Width="*" /> 
        </Grid.ColumnDefinitions> 
        <Border Background="Transparent"> 
         <Image Source="/ControlCSI;component/Images/GlossyBlack/bg_pulltab.png" /> 
        </Border> 
        <Grid Grid.Column="1" > 
         <Border CornerRadius="10,0,0,10" BorderBrush="#11c2f1" BorderThickness="0"> 
          <Border.Background> 
           <ImageBrush ImageSource="/ControlCSI;component/Images/GlossyBlack/bg_nav.png"/> 
          </Border.Background> 
          <ItemsPresenter /> 
         </Border> 
        </Grid> 
       </Grid> 
      </Border> 
     </Grid> 
    </ControlTemplate> 
</ItemsControl.Template>  

的标记定义,这里是实现:

<ControlCSI:SlideMenu x:Name="SlideOutMenu" Width="200" VerticalAlignment="Stretch" HorizontalAlignment="Right" RenderTransformOrigin="0.5,0.5" > 
      <ControlCSI:SlideMenu.RenderTransform> 
       <CompositeTransform TranslateX="180"/> 
      </ControlCSI:SlideMenu.RenderTransform> 

      <telerik:RadButton Margin="5,10,5,0" Click="RadButton_Click" Tag="30" Content="30 Days" Style="{StaticResource SliderButton}" /> 
      <telerik:RadButton Click="RadButton_Click" Tag="60" Content="60 Days" Style="{StaticResource SliderButton}"/> 
      <telerik:RadButton Click="RadButton_Click" Tag="90" Content="3 Months" Style="{StaticResource SliderButton}"/> 
      <telerik:RadButton Click="RadButton_Click" Tag="180" Content="6 Months" Style="{StaticResource SliderButton}"/> 
      <telerik:RadDatePicker x:Name="startDate" x:Uid="startDate" DropDownOpened="startDate_DropDownOpened" DropDownClosed="startDate_DropDownClosed" DateTimeWatermarkContent="Start Date" Style="{StaticResource SliderDatePicker}" Template="{StaticResource RadDateTimePickerControlTemplate1}" /> 
      <telerik:RadDatePicker DropDownOpened="startDate_DropDownOpened" DropDownClosed="startDate_DropDownClosed" DateTimeWatermarkContent="End Date" Style="{StaticResource SliderDatePicker}" Template="{StaticResource RadDateTimePickerControlTemplate1}" /> 


     </ControlCSI:SlideMenu> 

错误:

A first chance exception of type 'System.NullReferenceException' occurred in ControlCSI 
System.Windows.Data Error: BindingExpression path error: 'ErrorResources' property not found on 'ControlCSI.ResourceWrapper' 'ControlCSI.ResourceWrapper' (HashCode=42863040). BindingExpression: Path='ErrorResources.ErrorWindowTitle' DataItem='ControlCSI.ResourceWrapper' (HashCode=42863040); target element is 'ControlCSI.ErrorWindow' (Name=''); target property is 'Title' (type 'System.Object').. 
System.Windows.Data Error: BindingExpression path error: 'ErrorResources' property not found on 'ControlCSI.ResourceWrapper' 'ControlCSI.ResourceWrapper' (HashCode=42863040). BindingExpression: Path='ErrorResources.ErrorWindowGenericError' DataItem='ControlCSI.ResourceWrapper' (HashCode=42863040); target element is 'System.Windows.Controls.TextBlock' (Name='IntroductoryText'); target property is 'Text' (type 'System.String').. 
System.Windows.Data Error: BindingExpression path error: 'ErrorResources' property not found on 'ControlCSI.ResourceWrapper' 'ControlCSI.ResourceWrapper' (HashCode=42863040). BindingExpression: Path='ErrorResources.ErrorWindowErrorDetails' DataItem='ControlCSI.ResourceWrapper' (HashCode=42863040); target element is 'System.Windows.Controls.TextBlock' (Name='LabelText'); target property is 'Text' (type 'System.String').. 

x:name创建一个局部变量,所以我猜这是一个命名空间问题?

+0

我将自定义控件重新编码为一个带有ContentPresenter的UserControl,并且一切正常,直到我将x:name添加到控件。 – BentOnCoding 2011-03-05 00:11:41

+0

我收到完全相同的一组错误。 – BentOnCoding 2011-03-05 00:35:40

+0

删除'x:Uid'它什么都不做。如果您将“模板”属性的赋值移除并使用默认模板会发生什么? – AnthonyWJones 2011-03-05 17:34:04

回答

0

最后我切换到用户控件(这之后一些文件审查是正确的要走的路)。

我在控件中添加了按钮和日期选择器,并通过绑定到依赖属性来暴露它们。

0

我不认为你可以添加x:Name到那个控件,因为它已经提供了一个Name属性。查看[这个msdn链接]的注释部分。 1

然而,您可以像使用x:Name(我认为)一样使用Name属性。

最好的运气

添加链接,因为我不知道我所用的连接器的行为正确:P http://msdn.microsoft.com/en-us/library/cc189028%28v=vs.95%29.aspx

+0

其有趣的,你建议这....我真的刚刚完成看着,并产生相同的错误= \ – BentOnCoding 2011-03-05 01:05:21

+0

无赖:(我希望你能得到这个想出 – 2011-03-05 01:27:31

+0

x:名称是特别处理。如果控制在其上定义了一个'Name'属性,'x:Name'的值将被赋值给它,事实上,当Name属性在元素上可用时,没有任何区别使用'Name'或'x:Name''' x:Name'允许名称附加到元素,即使它们没有Name属性,所以像FindName这样的函数仍然可以工作,因此x:Name '最好只使用'Name'。 – AnthonyWJones 2011-03-05 17:32:18