gpt4 book ai didi

c# - 如何绑定(bind)或以其他方式获取和设置资源中控件的值?

转载 作者:可可西里 更新时间:2023-11-01 09:04:05 36 4
gpt4 key购买 nike

如何将用户控件资源内的控件绑定(bind)到属性?或者,我可以从后面的代码中找到控件并从那里获取和设置值吗?

这是我的标记。我已将其简化为相关部分:

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,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,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>

这是我的属性(property)。尽管双向绑定(bind),它始终为空:

Salesmen.xaml.cs:

string totalSalesmen { get; set; }

我听说过 VisualTreeHelperLogicalTreeHelper .这些可能会启用另一种方法——找到控件并手动获取它们。但是,VisualTreeHelper 只能看到 LayoutRoot 及其子项(不是 UserControl.Resources),而 LogicalTreeHelper 似乎不可用(这是一个 SilverLight 5 项目;我不知道 Silverlight 5 使用什么框架。我知道 LogicalTreeHelper 是仅适用于 4.5 及更高版本)

感谢您的帮助。 注意:此问题将获得 +50 赏金。系统要求我等待2天才能放赏金,所以我会放赏金并在2天后接受答案。在此之前,如果您的答案有效,我会通知您。

最佳答案

您对totalSalesmen绑定(bind) 以及EditAppointmentTemplate1 中的所有内容只要Template 就不会产生任何影响永远不会实例化。将 Template(ControlTemplate 和 DataTemplate)视为蓝图。内部定义的元素仅在某处使用模板时实例化。

你有什么用法吗?像这样:

<Grid>
...
<SchedulerDialog Template="{StaticResource EditAppointmentTemplate1}"/>
...
</Grid>

[编辑 #1]

好吧,让我们看看...您的双向 BindingtotalSalesmen 看起来不错,尽管有点臭。我认为属性 totalSalesmen 应该存在于 DataContext 中(这样绑定(bind)起来会更容易)。但首先让我们尝试让您的代码正常工作,也许我们稍后会做得更好:

问题

当(在一个单独的 xaml 文件中)在 Bindings 中使用 ElementName 同时使用模板定义 UI 的各个部分时(记住:模板中的内容仅在某处使用时创建,并且创建可能发生在不同的时间点)存在风险,您希望彼此了解的元素实际上是在不同的 NameScopes 中创建的.您受影响的 ElementName-Bindings 将不起作用。只是默默地行不通。

治愈

您可以尝试一个技巧:确保您有一个 StaticResource,其中包含对您最初希望由 ElementName 使用的元素的引用。然后您只需针对该 StaticResource 编写一个 Binding。看看我在这里做了什么:

<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]

当您绑定(bind)到 DataContext 的属性时,您只需指定路径而不是源(隐含的源将是 DataContext):

Text="{Binding Path=totalSalesmen, Mode=TwoWay}"

关于c# - 如何绑定(bind)或以其他方式获取和设置资源中控件的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30163447/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com