C# 绑定到自定义类中的元素
我有一个Telerik转换控件绑定到自定义类,转换控件接受图像作为输入。你能告诉我如何显式绑定,告诉元素名有图像引用吗。Itemsource是一个附加属性。我已经包括了下面的代码。我尝试了Path=AdImage,但没有成功 编辑:最近未成功的尝试,添加了数据模板:C# 绑定到自定义类中的元素,c#,wpf,xaml,telerik,C#,Wpf,Xaml,Telerik,我有一个Telerik转换控件绑定到自定义类,转换控件接受图像作为输入。你能告诉我如何显式绑定,告诉元素名有图像引用吗。Itemsource是一个附加属性。我已经包括了下面的代码。我尝试了Path=AdImage,但没有成功 编辑:最近未成功的尝试,添加了数据模板: <Grid Background="Black"> <telerik:RadTransitionControl x:Name="radControl" adRotator:AdRotatorEx
<Grid Background="Black">
<telerik:RadTransitionControl x:Name="radControl" adRotator:AdRotatorExtensions.ItemChangeDelay="0:0:3"
adRotator:AdRotatorExtensions.CurrentSelectedIndex="0"
adRotator:AdRotatorExtensions.IndexChanged="{Binding TopItemCommand, Mode=OneWay}"
adRotator:AdRotatorExtensions.ItemsSource="{Binding Path=ImagePaths}"
VerticalAlignment="Center"
HorizontalAlignment="Center" Width="650">
<telerik:RadTransitionControl.Transition>
<telerik:MotionBlurredZoomTransition />
</telerik:RadTransitionControl.Transition>
<telerik:RadTransitionControl.ContentTemplate>
<DataTemplate>
<Image Source="{Binding Path=ImagePaths.AdImage}" />
</DataTemplate>
</telerik:RadTransitionControl.ContentTemplate>
</telerik:RadTransitionControl>
</Grid>
<telerik:RadTransitionControl x:Name="radControl" adRotator:AdRotatorExtensions.ItemChangeDelay="0:0:3"
adRotator:AdRotatorExtensions.CurrentSelectedIndex="0"
adRotator:AdRotatorExtensions.IndexChanged="{Binding TopItemCommand, Mode=OneWay}"
adRotator:AdRotatorExtensions.ItemsSource="{Binding ImagePaths, Mode=OneWay,Path=AdImage}"
VerticalAlignment="Center"
HorizontalAlignment="Center" Width="650">
<telerik:RadTransitionControl.Transition>
<telerik:MotionBlurredZoomTransition />
</telerik:RadTransitionControl.Transition>
</telerik:RadTransitionControl>
附加属性的代码如下所示
您的AdImage属性包含要显示的图像的Uri?是的,AdImage属性包含要显示的图像的Uri以及类中的其他属性。我认为问题在于{Binding ImagePath,Mode=OneWay,Path=AdImage},您将绑定ImagePath对象的AdImage属性。我认为ImagePath是一个集合。也许在Telerik documentatin中,您会发现每个绑定到RadTransitionControl的对象都有一种模板,也许您的解决方案是
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.RegisterAttached("ItemsSource", typeof(IEnumerable), typeof(AdRotatorExtensions), new PropertyMetadata(null, OnItemsSourceChanged));
private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var element = d as FrameworkElement;
var itemsControl = d as ItemsControl; // this is always null it returns a RadTranisitionControl object which doesnt have DisplayMemberName property
var oldValue = e.OldValue as IEnumerable;
var newValue = e.NewValue as IEnumerable;
if (element != null)
{
if (oldValue != null)
{
// Detach the Ad Rotator functionality.
element.Loaded -= OnElementLoaded;
element.Unloaded -= OnElementUnloaded;
// If there is a timer attached, stop it.
var timer = GetTimer(element);
if (timer != null)
{
timer.Stop();
}
}
if (newValue != null)
{
// Attach the Ad Rotator functionality.
element.Loaded += OnElementLoaded;
element.Unloaded += OnElementUnloaded;
// If the target is an ItemsControl and its ItemsSource is not set, set it.
if (itemsControl != null && itemsControl.ItemsSource == null && itemsControl.Items.Count == 0)
{
itemsControl.ItemsSource = newValue;
itemsControl.DisplayMemberPath = "AdImage"; // will never reaches here
}
}
}
}
private static void OnElementLoaded(object sender, RoutedEventArgs args)
{
var element = sender as DependencyObject;
// Create the timer and hook-up to the events.
var timer = new DispatcherTimer();
timer.Interval = GetItemChangeDelay(element).TimeSpan;
SetTimer(element, timer);
timer.Tick += (s, e) => MoveToNextElement(element);
timer.Start();
// Make sure the currently pointed element is selected.
UpdateCurrentlySelectedItem(element);
}
private static void UpdateCurrentlySelectedItem(DependencyObject element)
{
var contentControl = element as ContentControl;
var source = GetItemsSource(element);
// If there is no source we shouldn't do anything.
if (source == null) return;
// Find the actual index to be selected (if outside the boundaries of the collection)
// and find the actual element to be selected.
var convertedSource = source.Cast<object>();
var currentIndex = GetCurrentSelectedIndex(element);
var elementToSelect = convertedSource.ElementAtOrDefault(currentIndex);
ICommand updateValue = null;
if (contentControl != null)
{
updateValue = contentControl.GetValue(IndexChangedProperty) as ICommand;
}
if (updateValue != null)
if (updateValue.CanExecute(elementToSelect))
{
updateValue.Execute(elementToSelect);
}
// Update the cotnent of the ContentControl if attached to a ContentControl.
if (contentControl != null)
{
contentControl.Content = elementToSelect;
}
}