我在我的WPF MVVM应用程序中使用RadCarousel控件,我必须加载一组图像以供查看。当我加载最少5张图像时,控件工作正常。如果我尝试加载低于5的数量,则图像不会按预期查看。但是在按下按钮后,我可以看到图像。此外,我必须在这些图像集中设置默认图像。
XAML代码:
<telerik:RadWindow x:Class="Test.Presentation.View.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:ViewModel="clr-namespace:Test.Presentation.ViewModel"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Height="600" Width="600" WindowStartupLocation="CenterScreen">
<telerik:RadWindow.Resources>
<ViewModel:MarketSeriesViewModel x:Key="ViewModel1"/>
</telerik:RadWindow.Resources>
<Grid DataContext="{StaticResource ViewModel1}">
<GroupBox>
<Grid >
<Grid.Resources>
<Path x:Key="path"
Data="M30,347 L307.5,347"
Stroke="#FF0998f8"
StrokeThickness="2"
Stretch="Fill" />
<Style TargetType="{x:Type telerik:CarouselItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type telerik:CarouselItem}">
<Grid x:Name="grid"
Height="190"
HorizontalAlignment="Left"
Width="250">
<Border BorderThickness="1"
CornerRadius="3" BorderBrush="#cecece">
<Border BorderThickness="0"
CornerRadius="0"
Margin="0">
<Border.BorderBrush>
<LinearGradientBrush EndPoint="0.5,1"
StartPoint="0.5,0">
<GradientStop Color="White"
Offset="0.13" />
</LinearGradientBrush>
</Border.BorderBrush>
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1"
StartPoint="0.5,0">
<GradientStop Color="White"
Offset="0.26" />
</LinearGradientBrush>
</Border.Background>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="24" />
<RowDefinition />
</Grid.RowDefinitions>
<Border Grid.Row="0"
BorderThickness="0"
Margin="0"
VerticalAlignment="Top"
Height="24"
CornerRadius="2,2,0,0">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1"
StartPoint="0.5,0">
<GradientStop Color="#21a2de"
Offset="1" />
<GradientStop Color="#21a2de"
Offset="0.1" />
<GradientStop Color="#21a2de"
Offset="0.57" />
<GradientStop Color="#21a2de"
Offset="0.11" />
</LinearGradientBrush>
</Border.Background>
<Border.BorderBrush>
<LinearGradientBrush EndPoint="1,0.5"
StartPoint="0,0.5">
<GradientStop Offset="0" />
<GradientStop Offset="1" />
<GradientStop Color="White"
Offset="0.5" />
</LinearGradientBrush>
</Border.BorderBrush>
<StackPanel x:Name="stackPanel"
Orientation="Horizontal"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Margin="6,0,0,0">
<TextBlock Text="{Binding RDNumber}"
TextWrapping="Wrap"
Foreground="White"
FontSize="11"
TextAlignment="Center"/>
</StackPanel>
</Border>
<StackPanel Grid.Row="1"
Orientation="Horizontal">
<Image VerticalAlignment="Center"
HorizontalAlignment="Center"
Source="{Binding ImagePath}"
Stretch="Fill"
Height="160" Width="250" />
</StackPanel>
</Grid>
</Border>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBox x:Name="txtInput" Grid.Row="0" Width="100" HorizontalAlignment="Left"/>
<telerik:RadButton Grid.Row="0" Width="100" Content="Load" Margin="100,0,0,0" Click="RadButton_Click"/>
<telerik:RadCarousel x:Name="radCarousel1" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding ImageCollectionById, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Background="Transparent" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Hidden"
VirtualizingStackPanel.IsVirtualizing="False" telerik:StyleManager.Theme="Windows8" Focusable="True">
<telerik:RadCarousel.ContextMenu>
<ContextMenu>
<MenuItem Header="Edit" Command="{Binding EditImage}" CommandParameter="{Binding}"/>
<MenuItem Header="MetaData"/>
<MenuItem Header="Delete" Command="{Binding DeleteImage}"/>
</ContextMenu>
</telerik:RadCarousel.ContextMenu>
<telerik:RadCarousel.ItemsPanel>
<ItemsPanelTemplate>
<telerik:RadCarouselPanel Path="{StaticResource path}" telerik:StyleManager.Theme="Windows8" AutoLoadItems="True" IsSelectedTopItem="True">
</telerik:RadCarouselPanel>
</ItemsPanelTemplate>
</telerik:RadCarousel.ItemsPanel>
</telerik:RadCarousel>
</Grid>
</Grid>
</GroupBox>
</Grid>
</telerik:RadWindow>
Xmal.cs代码:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Navigation;
namespace Test.Presentation.View
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : RadWindow
{
public Window1()
{
InitializeComponent();
RadWindowInteropHelper.SetShowInTaskbar(this, true);
m_cImageCollection = new ObservableCollection<ImageClass>();
m_cImageCollectionbyId = new ObservableCollection<ImageClass>();
LoadImageCollection();
}
private ObservableCollection<ImageClass> m_cImageCollection;
private ObservableCollection<ImageClass> m_cImageCollectionbyId;
public ObservableCollection<ImageClass> ImageCollection
{
get { return this.m_cImageCollection; }
set
{
if (this.m_cImageCollection != value)
{
this.m_cImageCollection = value;
}
}
}
public ObservableCollection<ImageClass> ImageCollectionById
{
get { return this.m_cImageCollectionbyId; }
set
{
if (this.m_cImageCollectionbyId != value)
{
this.m_cImageCollectionbyId = value;
}
}
}
private void LoadImageCollection()
{
try
{
ImageClass NewImage = new ImageClass();
NewImage.Id = 1;
NewImage.ImagePath = @"U:\My Documents\Projects\Images\Chair.jpeg";
NewImage.IsDefault = true;
ImageCollection.Add(NewImage);
NewImage = new ImageClass();
NewImage.Id = 1;
NewImage.ImagePath = @"U:\My Documents\Projects\Images\Cot1.jpeg";
NewImage.IsDefault = false;
ImageCollection.Add(NewImage);
NewImage = new ImageClass();
NewImage.Id = 1;
NewImage.ImagePath = @"U:\My Documents\Projects\Images\DiningSet1.jpeg";
NewImage.IsDefault = false;
ImageCollection.Add(NewImage);
NewImage = new ImageClass();
NewImage.Id = 1;
NewImage.ImagePath = @"U:\My Documents\Projects\Images\Havana restaurant chair.jpeg";
NewImage.IsDefault = false;
ImageCollection.Add(NewImage);
NewImage = new ImageClass();
NewImage.Id = 1;
NewImage.ImagePath = @"U:\My Documents\Projects\Images\Sofa1.jpeg";
NewImage.IsDefault = false;
ImageCollection.Add(NewImage);
NewImage = new ImageClass();
NewImage.Id = 1;
NewImage.ImagePath = @"U:\My Documents\Projects\Images\Sofa2.jpeg";
NewImage.IsDefault = false;
ImageCollection.Add(NewImage);
NewImage = new ImageClass();
NewImage.Id = 2;
NewImage.ImagePath = @"U:\My Documents\Projects\Images\Sofa3.jpeg";
NewImage.IsDefault = false;
ImageCollection.Add(NewImage);
NewImage = new ImageClass();
NewImage.Id = 2;
NewImage.ImagePath = @"U:\My Documents\Projects\Images\StudyRoomSet.jpeg";
NewImage.IsDefault = false;
ImageCollection.Add(NewImage);
NewImage = new ImageClass();
NewImage.Id = 2;
NewImage.ImagePath = @"U:\My Documents\Projects\Images\TVStand1.jpeg";
NewImage.IsDefault = false;
ImageCollection.Add(NewImage);
NewImage = new ImageClass();
NewImage.Id = 3;
NewImage.ImagePath = @"U:\My Documents\Projects\Images\Wadroop1.jpeg";
NewImage.IsDefault = false;
ImageCollection.Add(NewImage);
NewImage = new ImageClass();
NewImage.Id = 4;
NewImage.ImagePath = @"U:\My Documents\Projects\Images\Wall1.jpeg";
NewImage.IsDefault = false;
ImageCollection.Add(NewImage);
NewImage = new ImageClass();
NewImage.Id = 4;
NewImage.ImagePath = @"U:\My Documents\Projects\Images\Sofa1.jpeg";
NewImage.IsDefault = false;
ImageCollection.Add(NewImage);
NewImage = new ImageClass();
NewImage.Id = 5;
NewImage.ImagePath = @"U:\My Documents\Projects\Images\Sofa3.jpeg";
NewImage.IsDefault = false;
ImageCollection.Add(NewImage);
}
catch (Exception)
{
throw;
}
}
private void RadButton_Click(object sender, RoutedEventArgs e)
{
var ss = txtInput.Text;
ImageCollectionById = new ObservableCollection<ImageClass>( ImageCollection.Where(x => x.Id == Convert.ToInt32(ss)).ToList());
radCarousel1.ItemsSource = ImageCollectionById;
}
}
public class ImageClass
{
private Int32 m_iId;
private string m_sImagePath;
private bool m_bIsDefault;
public Int32 Id
{
get { return this.m_iId; }
set
{
if (this.m_iId != value)
{
this.m_iId = value;
}
}
}
public string ImagePath
{
get { return this.m_sImagePath; }
set
{
if (this.m_sImagePath != value)
{
this.m_sImagePath = value;
}
}
}
public bool IsDefault
{
get { return this.m_bIsDefault; }
set
{
if (this.m_bIsDefault != value)
{
this.m_bIsDefault = value;
}
}
}
}
}
供参考:
下面的代码,它的工作正如我预期的那样。此代码用于xmal.cs
文件。但我需要MVVM模式中的这种功能而没有附加行为。
radcarousel1.BringDataIntoView(Item)