I am trying to databing a datatable result in XAML (declarative databinding). I tried it programmatically and it does work by setting the Itemsource of my radcarousel to the datatable in code. Can you please tell me where i am going wrong with my ObjectDataProvider.
XAML code:
| <Window x:Class="TelerikCarousel.Window1" |
| xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
| xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
| xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" |
| xmlns:carousel="clrnamespace:Telerik.Windows.Controls.Carousel;assembly=Telerik.Windows.Controls.Navigation" |
| xmlns:telerikT="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls" |
| xmlns:local="clr-namespace:TelerikCarousel" |
| Title="Window1" Height="800" Width="800" Background="DarkBlue"> |
| <Window.Resources> |
| <ObjectDataProvider x:Key="objectDataProvider" ObjectType="{x:Type local:Window1}" MethodName="InserRow" /> |
| </Window.Resources> |
| <Grid> |
| <Grid.RowDefinitions> |
| <RowDefinition Height="280*" /> |
| <RowDefinition Height="482*" /> |
| </Grid.RowDefinitions> |
| <Grid.Resources> |
| <!-- removed the carousel stylings so it could be shorter but it does work --> |
| </GridResources> |
| <telerik:RadCarousel Margin="19,51,22,293" Name="radCarousel1" ItemsSource="{Binding Source={StaticResource objectDataProvider}}" Grid.RowSpan="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" > |
| </telerik:RadCarousel> |
| </Grid> |
| </Window> |
c# Code:
| using System; |
| using System.Windows; |
| using System.Windows.Media; |
| using System.Windows.Shapes; |
| using System.Data.SqlClient; |
| using System.Data; |
| using Telerik.Windows.Controls; |
| namespace TelerikCarousel |
| { |
| /// <summary> |
| /// Interaction logic for Window1.xaml |
| /// </summary> |
| public partial class Window1 : Window |
| { |
| public Window1() |
| { |
| InitializeComponent(); |
| //object itemsSource = radCarousel1.ItemsSource; |
| this.radCarousel1.Loaded += new RoutedEventHandler(radCarousel1_Loaded); |
| } |
| void radCarousel1_Loaded(object sender, RoutedEventArgs e) |
| { |
| Path path = CreateLinePath(); |
| RadCarouselPanel panel = this.radCarousel1.FindCarouselPanel(); |
| panel.ItemsPerPage = 5; |
| panel.Path = path; |
| this.radCarousel1.ReflectionSettings.Visibility = Visibility.Visible; |
| this.radCarousel1.ReflectionSettings.Opacity = 0.5; |
| } |
| private Path CreateLinePath() |
| { |
| Path newPath = new Path(); |
| PathFigureCollectionConverter figureConverter = new PathFigureCollectionConverter(); |
| object geometryFigures = figureConverter.ConvertFromString("M30,347 L307.5,347"); |
| PathGeometry newGeometry = new PathGeometry(); |
| newPath.Stretch = Stretch.Fill; |
| BrushConverter brushConverter = new BrushConverter(); |
| newPath.Stroke = (Brush)brushConverter.ConvertFromString("#FF0998f8"); |
| newPath.StrokeThickness = 2; |
| newGeometry.Figures = (PathFigureCollection)geometryFigures; |
| newPath.Data = (Geometry)newGeometry; |
| return newPath; |
| } |
| //public DataSet dataset = new DataSet("MedSpaImage"); |
| public DataTable dt = new DataTable("Medxx"); |
| public DataTable InserRow() |
| { |
| string myConnectionstring = ""; |
| if (myConnectionstring == "") |
| { |
| myConnectionstring = "Initial Catalog=MedSpa;Data Source=4DBQR61\\IMSERVER;Integrated Security=True"; |
| } |
| try |
| { |
| SqlConnection myConnection = new SqlConnection(myConnectionstring); |
| string myInsertQuery = "SELECT * FROM Testing"; |
| SqlCommand myCommand = new SqlCommand(myInsertQuery); |
| myCommand.Connection = myConnection; |
| SqlDataAdapter adapter = new SqlDataAdapter(); |
| myConnection.Open(); |
| adapter.SelectCommand = new SqlCommand(myInsertQuery, myConnection); |
| adapter.Fill(dt); |
| myCommand.ExecuteNonQuery(); |
| myCommand.Connection.Close(); |
| } |
| catch (Exception ex) |
| { |
| MessageBox.Show(ex.ToString()); |
| } |
| return dt; |
| } |
| } |
| } |
I am just playing with a datable. My ultimate goal would be to be able to databind in XAML the carousel with a property of type ObservableCollection<>. Is that even possible? Thanks