我在将x:name控件添加到自定义控件时遇到问题。
添加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 :(得分:0)
我不认为您可以将x:Name添加到该控件,因为它已经提供了Name属性。查看[this msdn link]的备注部分。1
然而,你可以像使用x:Name(我想)一样使用Name属性。
祝你好运
添加链接原因我不确定我是否正确使用了链接行为:P http://msdn.microsoft.com/en-us/library/cc189028%28v=vs.95%29.aspx
答案 1 :(得分:0)
最后我切换到UserControl(经过一些文档审核后,这是正确的方法)。
我在控件内部添加了按钮和日期选择器,并通过绑定到依赖项属性来公开它们。