Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Loading XML with webclient to use as style for datagrid
2 replies. Latest Post by Amanda Wang - MSFT on July 7, 2009.
(0)
Jya
Member
0 points
4 Posts
07-03-2009 5:12 AM |
I have an XML-file which contains a style and is located under the Clientbin-directory.
<Style xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Key="MyCustomRow" xmlns:datagrid="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" TargetType="datagrid:DataGridRow" xmlns:localprimitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data" xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows" > <Setter Property="IsTabStop" Value="False" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="datagrid:DataGridRow"> <localprimitives:DataGridFrozenGrid Name="Root"> <vsm:VisualStateManager.VisualStateGroups> <vsm:VisualStateGroup x:Name="CommonStates"> <vsm:VisualStateGroup.Transitions> <vsm:VisualTransition GeneratedDuration="0" /> </vsm:VisualStateGroup.Transitions> <vsm:VisualState x:Name="Normal" /> <vsm:VisualState x:Name="Normal AlternatingRow"> <Storyboard> <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="0"/> </Storyboard> </vsm:VisualState> <vsm:VisualState x:Name="MouseOver"> <Storyboard> <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To=".5"/> <ColorAnimationUsingKeyFrames BeginTime="0" Duration="0" Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"> <SplineColorKeyFrame KeyTime="0" Value="#66ccff"/> </ColorAnimationUsingKeyFrames> </Storyboard> </vsm:VisualState> <vsm:VisualState x:Name="Normal Selected"> <Storyboard> <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/> <ColorAnimationUsingKeyFrames BeginTime="0" Duration="0" Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"> <SplineColorKeyFrame KeyTime="0" Value="#66ccff"/> </ColorAnimationUsingKeyFrames> </Storyboard> </vsm:VisualState> <vsm:VisualState x:Name="MouseOver Selected"> <Storyboard> <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/> <ColorAnimationUsingKeyFrames BeginTime="0" Duration="0" Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"> <SplineColorKeyFrame KeyTime="0" Value="#66ccff"/> </ColorAnimationUsingKeyFrames> </Storyboard> </vsm:VisualState> <vsm:VisualState x:Name="Unfocused Selected"> <Storyboard> <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/> <ColorAnimationUsingKeyFrames BeginTime="0" Duration="0" Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"> <SplineColorKeyFrame KeyTime="0" Value="#66ccff"/> </ColorAnimationUsingKeyFrames> </Storyboard> </vsm:VisualState> </vsm:VisualStateGroup> </vsm:VisualStateManager.VisualStateGroups> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid.Resources> <Storyboard x:Key="DetailsVisibleTransition"> <DoubleAnimation Storyboard.TargetName="DetailsPresenter" Storyboard.TargetProperty="ContentHeight" Duration="00:00:0.1" /> </Storyboard> </Grid.Resources> <Rectangle x:Name="BackgroundRectangle" Grid.RowSpan="2" Grid.ColumnSpan="2" Opacity="0" Fill="#1ca0f2"/> <localprimitives:DataGridRowHeader Grid.RowSpan="3" Name="RowHeader" localprimitives:DataGridFrozenGrid.IsFrozen="True" /> <localprimitives:DataGridCellsPresenter Grid.Column="1" Name="CellsPresenter" localprimitives:DataGridFrozenGrid.IsFrozen="True" /> <localprimitives:DataGridDetailsPresenter Grid.Row="1" Grid.Column="1" Name="DetailsPresenter" /> <Rectangle Grid.Row="2" Grid.Column="1" Name="BottomGridLine" HorizontalAlignment="Stretch" Height="1" /> </localprimitives:DataGridFrozenGrid> </ControlTemplate> </Setter.Value> </Setter></Style>
The xml-file is downloaded as followed.
var webClient = new WebClient(); webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted); webClient.DownloadStringAsync(new Uri("DatagridRowStyle.xml", UriKind.RelativeOrAbsolute));
After this i convert my result to a style as followed
string XmlString = e.Result;XElement elem = XElement.Load(XmlReader.Create(new StringReader(XmlString)));s = System.Windows.Markup.XamlReader.Load((elem).ToString()) as Style;
but when I assign this style to my datagrid it gives me an error "xamlparseexception occured" at line 0, position 0
After this I tried exactly the same with a regular button and gave it this style and this works
<Style xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Key="btnTest" TargetType="Button"> <Setter Property="Background" Value="Red"/> <Setter Property="FontSize" Value="25"/> <Setter Property="Foreground" Value="Blue"/></Style>
can someone tell me what i'm doing wrong?
codebased
Participant
806 points
360 Posts
07-04-2009 5:28 AM |
Do you have this in the project already and registered with the same namespace?
xmlns:datagrid="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
Amanda W...
All-Star
17234 points
1,466 Posts
07-07-2009 6:07 AM |
Hi,
Jya: can someone tell me what i'm doing wrong?
The cause of your problem is on the line code XElement elem = XElement.Load(XmlReader.Create(new StringReader(XmlString)));
You can try to set a break point on this line code, you will notice elem content includes the content of the xml and the attributes' values, in other word the elem does not have the correct xml format conent. That's why the XamlReader.Load does not the parse the xml correctly.
You can try to refer below code, use XDocument instead of XElement:
void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { string XmlString = e.Result; XDocument doc = XDocument.Load(new StringReader(XmlString));
// XElement elem = XElement.Load(XmlReader.Create(new StringReader(XmlString))); // XElement elem = XElement.Load(XmlReader.Create(XmlString))); Style s = System.Windows.Markup.XamlReader.Load((doc).ToString()) as Style; }