Salesmen.xaml:
<UserControl.Resources> <ControlTemplate x:Key="EditAppointmentTemplate1" TargetType="local:SchedulerDialog" x:Name="ControlTemplate"> <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"> <Grid> <Grid Name="grdTotal" Grid.Row="4" Visibility="{Binding ResourceTypesVisibility}"> <TextBox x:Name="totalSalesmen" Grid.Row="0" Grid.Column="1" Margin="3" Width="120" Text="{Binding Parent.totalSalesmen,ElementName=LayoutRoot,Mode=TwoWay}" /> </Grid> </ScrollViewer> </ControlTemplate> <Style x:Key="EditAppointmentDialogStyle1" TargetType="local:SchedulerDialog"> <Setter Property="Template" Value="{StaticResource EditAppointmentTemplate1}" /> </Style> </UserControl.Resources> <Grid x:Name="LayoutRoot" Background="White" HorizontalAlignment="Left" Margin="10,10,0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <StackPanel Orientation="Vertical"> <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" BorderBrush="Transparent"> <telerik:RadCalendar Name="RadCalendar" SelectedDate="{Binding CurrentDate,ElementName=RadScheduleViewTests,Mode=TwoWay}" IsTodayHighlighted="True" telerik:StyleManager.Theme="Metro" HorizontalAlignment="Left" VerticalAlignment="Top" FirstDayOfWeek="Sunday" Margin="0,15,0" SelectionChanged="RadCalendar_SelectionChanged_1" > </telerik:RadCalendar> </ScrollViewer> </StackPanel> <telerik:RadScheduleView Name="RadScheduleViewTests" MinAppointmentWidth="100" Tag="{Binding Path=Context,ElementName=TestDayPage}" telerik:StyleManager.Theme="Metro" Grid.Column="1" EditAppointmentDialogStyle="{StaticResource EditAppointmentDialogStyle1}" AppointmentCreating="RadScheduleViewTests_AppointmentCreating_1" AppointmentEditing="RadScheduleViewTests_AppointmentEditing_1" AppointmentDeleting="RadScheduleViewTests_AppointmentDeleting_1" FirstDayOfWeek="Sunday" ShowDialog="RadScheduleViewTests_ShowDialog_1" AppointmentEdited="RadScheduleViewTests_AppointmentEdited_1"> <telerik:RadScheduleView.DragDropBehavior> <examiners:CustomDragDropBehavIoUr/> </telerik:RadScheduleView.DragDropBehavior> <telerik:RadScheduleView.SchedulerDialogHostFactory> <test:CustomScheduleViewDialogHostFactory /> </telerik:RadScheduleView.SchedulerDialogHostFactory> <telerik:RadScheduleView.ViewDefinitions> <telerik:DayViewDefinition/> <telerik:WeekViewDefinition/> <telerik:MonthViewDefinition/> <telerik:TimelineViewDefinition/> </telerik:RadScheduleView.ViewDefinitions> </telerik:RadScheduleView> </Grid>
这是我的财产.尽管双向绑定,但它始终为空:
Salesmen.xaml.cs:
string totalSalesmen { get; set; }
我听说过VisualTreeHelper和LogicalTreeHelper.这些可能会启用另一种方法 – 手动找到控制和获取.但是,VisualTreeHelper只看到LayoutRoot和它的孩子(而不是UserControl.Resources),LogicalTreeHelper似乎不可用(它是一个SilverLight 5项目;我不知道Silverlight 5使用了什么框架.我知道LogicalTreeHelper是仅在4.5及更晚版本可用)
谢谢你的帮助.注意:这个问题会得到50的赏金.该制度要求我等待2天的赏金,所以我会把赏金收回2天后接受答复.如果你的答案在此之前工作,我会让你知道.
解决方法
将模板(ControlTemplate和DataTemplate)都视为蓝图.内部定义的元素仅在模板在某处使用时才被实例化.
你有什么用处吗喜欢这个:
<Grid> ... <SchedulerDialog Template="{StaticResource EditAppointmentTemplate1}"/> ... </Grid>
[编辑#1]
好吧,看看…你的twoway绑定到totalSalesmen看起来很好,虽然有点臭.我认为属性totalSalesmen应该应该生活在DataContext(并且更容易绑定).
但是首先让我们尝试使你的代码工作,或许我们稍后会很好:
问题
当(在一个单个xaml文件中)在Bindings中使用ElementName,同时使用模板定义UI的部分(并记住:模板中的内容仅在某处使用时创建,并且创建可能发生在不同的位置时间点)有风险,您期望相互认识的元素实际上是以不同的NameScopes创建的.而您受影响的ElementName-Bindings将无法正常工作.只是默默地不会工作.
治愈
您可以尝试一个诀窍:确保您有一个StaticResource,其中包含您最初要由ElementName使用的元素的引用.
然后你只是写一个绑定对该StaticResource.看看我在这里做了什么
<UserControl x:Class="Salesmen" ... x:Name="me"> <UserControl.Resources> <BindableObjectReference x:Key="MyUserControl" Object="{Binding ElementName=me}"/> <ControlTemplate x:Key="EditAppointmentTemplate1" TargetType="local:SchedulerDialog"> ... <TextBox Text="{Binding Path=Object.totalSalesmen,Source={StaticResource MyUserControl},Mode=TwoWay}"/> ... </ControlTemplate> </UserControl.Resources>
和代码
public class BindableObjectReference : DependencyObject { public object Object { get { return GetValue( ObjectProperty ); } set { SetValue( ObjectProperty,value ); } } public static readonly DependencyProperty ObjectProperty = DependencyProperty.Register( "Object",typeof( object ),typeof( BindableObjectReference ),new PropertyMetadata( null ) ); }
[编辑#2]
当绑定到DataContext的属性时,您只需指定路径,但不指定源(隐式地将源为DataContext):
Text="{Binding Path=totalSalesmen,Mode=TwoWay}"