How to use vector XAML files? - xaml
I have 93 SVG files which I converted to XAML using XamlTune and following these instructions. The resulting files place the vector information into a Canvas.
I know need to how to use these files in my application. Do I need to put these into a ResourceDictionary, or can I access them directly from the app package?
An example XAML file:
<Canvas Name="Layer_1" Width="20" Height="20" ClipToBounds="True" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"><Canvas><Path Fill="#FF222222"><Path.Data><PathGeometry FillRule="Nonzero" Figures="M4.3,0.025C3.776,0.025,3.35,0.451,3.35,0.975L3.35,4.775C3.35,5.299 3.776,5.725 4.3,5.725 4.824,5.725 5.25,5.299 5.25,4.775L5.25,0.975C5.25,0.451,4.824,0.025,4.3,0.025z" /></Path.Data></Path><Path Fill="#FF222222"><Path.Data><PathGeometry FillRule="Nonzero" Figures="M15.7,0.025C15.176,0.025,14.75,0.451,14.75,0.975L14.75,4.775C14.75,5.299 15.176,5.725 15.7,5.725 16.224,5.725 16.65,5.299 16.65,4.775L16.65,0.975C16.65,0.451,16.224,0.025,15.7,0.025z" /></Path.Data></Path><Path Fill="#FF222222"><Path.Data><PathGeometry FillRule="Nonzero" Figures="M18.55,3.35L17.6,3.35 17.6,4.775C17.6,5.82 16.745,6.675 15.7,6.675 14.655,6.675 13.8,5.82 13.8,4.775L13.8,3.35 6.2,3.35 6.2,4.775C6.2,5.82 5.345,6.675 4.3,6.675 3.255,6.675 2.4,5.82 2.4,4.775L2.4,3.35 1.45,3.35C0.926,3.35,0.5,3.776,0.5,4.3L0.5,7.15 0.5,8.575 1.925,8.575 18.075,8.575 19.5,8.575 19.5,7.15 19.5,4.3C19.5,3.776,19.074,3.35,18.55,3.35z" /></Path.Data></Path><Path Fill="#FF222222"><Path.Data><PathGeometry FillRule="Nonzero" Figures="M0.5,9.525L0.5,19.025C0.5,19.549,0.926,19.975,1.45,19.975L18.55,19.975C19.074,19.975,19.5,19.549,19.5,19.025L19.5,9.525 0.5,9.525z M17.6,18.075L2.4,18.075 2.4,11.425 17.6,11.425 17.6,18.075z" /></Path.Data></Path></Canvas></Canvas>
I suppose I understand what you are asking. Let's start that using them in a ResourceDictionary is perfectly fine, and might be ideal. The second, most obvious use, would be to put them in a UserControl. Both options would allow you to reuse them however you want.
Let's pretend I had a Canvas and I wanted to do just that.
<Canvas Height="100" Width="100" Background="Blue" />
Now, to use a ResourceDictionary, I would do this:
<Grid>
<Grid.Resources>
<DataTemplate x:Name="MyShape">
<Canvas Height="100" Width="100" Background="Red" />
</DataTemplate>
</Grid.Resources>
<ContentPresenter Content="{x:Null}" ContentTemplate="{StaticResource MyShape}" />
</Grid>
Note you can remove this from Resources and move it to a file if you like.
And, if you want a user control which gives you advantage of code-behind, you can do that, too.
Best of luck.
Related
Is there a way to remove or customize Pivot transitions?
By default, Pivots transition their PivotItems in/out using a horizontal translation with an opacity fade. I'd like a way to remove them (firstly), but also customize if possible. Things I've tried to remove them (just passing an empty TransititionCollection in): <Pivot> <Pivot.Transitions> <TransitionCollection /> </Pivot.Transitions> <Pivot.ItemContainerTransitions> <TransitionCollection /> </Pivot.ItemContainerTransitions> <PivotItem Header="Red"> <PivotItem.Transitions> <TransitionCollection /> </PivotItem.Transitions> <PivotItem.ContentTransitions> <TransitionCollection /> </PivotItem.ContentTransitions> <Grid Background="Red" Width="100" Height="200" /> </PivotItem> <PivotItem Header="Green"> <Grid Background="Green" Width="100" Height="200" /> </PivotItem> </Pivot>
If you dig into the default Pivot template, you will find the following part for the content: <ItemsPresenter x:Name="PivotItemPresenter" Grid.ColumnSpan="3" Grid.Row="1"> <ItemsPresenter.RenderTransform> <TransformGroup> <TranslateTransform x:Name="ItemsPresenterTranslateTransform"/> <CompositeTransform x:Name="ItemsPresenterCompositeTransform"/> </TransformGroup> </ItemsPresenter.RenderTransform> </ItemsPresenter> These two Transforms inside the TransformGroup manage the animation of the items, there is no Transition behind it. This means that setting TransitionCollection empty has unfortunately no effect. Even more unfortunate is the fact that you cannot easily remove the transition, because trying trying to remove either of the two Transforms from the Pivot style will make the control not render properly. This is likely because both transforms are referenced within the controls code-behind. As a result, it seems that customizing the animation of the built-in Pivot is unfortunately not possible.
How to create WPF UserControl having a Canvas as the "main panel"?
I want to create a UserControl having a Canvas inside it. Then I can use it like this in XAML: <Window ...> <Grid> <local:MyCanvasLikeControl> <Path .../> <Path .../> <Polygon.../> </local:MyCanvasLikeControl> </Grid> </Window> I have tried some things, but always get this error: "Property Content can only be set once". I know of ControlTemplates and such, but could not find my way by reading the docs by myself. My goal is to have an equivalent to this: <Window ...> <Grid ...> <Border ....> <Canvas ...> <Path ...> <Path ...> <Polygon ...> </Canvas> </Border> </Grid> </Window> But moving the "Border / Canvas" to a UserControl named "MyCanvasLikeControl"
It's the same type of scenario as if you to stuff a bunch of elements of any other type into a control that would produce the same error like for example; <UserControl> <Grid/> <StackPanel/> <Canvas/> <!-- you get the idea --> </UserControl> It's just telling you that you need to specify a parent capable of holding your content to act as the single content of its parent, which in this case would be your control. So to fix this, it's luckily simple; <local:MyCanvasLikeControl> <Grid> <Path .../> <Path .../> <Polygon.../> </Grid> </local:MyCanvasLikeControl> Or switch Grid with Canvas or whatever you want so long as it can host children elements. Hope this helps. EDIT: Ok, so I think the definition of requirement may have been a bit over-complicated in its explanation. If I'm understanding you correctly then here's why you're breaking. You have your first layer of elements that contain your UserControl, but the way you're trying to insert content into it isn't the way that works, you'll need to provide that ability by specifying how you wish to allow it to be added to your control via a ContentPresenter or ContentControl etc. So instead it would be more like this inside your external UserControl; <Border ....> <Canvas ...> <ContentPresenter/> or <ContentControl/> etc. </Canvas> </Border> So then you can inject that stuff in there the way you want. <Window ...> <Grid> <local:MyCanvasLikeControl> <local:MyCanvasLikeControl.Content> <Path .../> <Path .../> <Polygon.../> </local:MyCanvasLikeControl.Content> </local:MyCanvasLikeControl> </Grid> </Window> Make sense?
DataTemplate does not show result of bound children
On my MainWindow.xaml page, I have the following code which works (where MyWord is a string) <ContentControl Content="{Binding MyWord}" /> I'm playing with DataTemplates and trying understand them. So, I want to reference a DataTemplate from within my ContentControl. The DataTemplate should contain a TextBlock which binds to my string. I updated my code <ContentControl ContentTemplate="{StaticResource ViewsTemplate}" /> And in my ResourceDictionary I add <DataTemplate x:Key="ViewsTemplate"> <TextBlock Text="{Binding MyWord}" /> </DataTemplate> This produces no text on screen at all. I even tried <ContentControl Content="{Binding MyWord}" ContentTemplate="{StaticResource ViewsTemplate}" /> and still no result on screen. I can't work out why can any one give some advice please. Thank you
The ContentControl still needs to have some content bound to it. <ContentControl ContentTemplate="{StaticResource ViewsTemplate}" Content="{Binding MyWord}" /> Would work, but then you'd need to change your data template because it expects to be able to find MyWord which of course it won't be able to, so you'd want to use just {Binding} instead. Alternatively, bind the ContentControl's Content to {Binding} - the current DataContext of its parent - and leave the template as it is.
silverlight 4.0 scrollviewer not updating
I am new to Silverlight. I am trying to provide a zoom in functionality along with inkpresenter. I am using Silverlight 4, c#, asp.net. I can bind the slider to canvas and it does zoom in but i am facing problem with the scrollviewer layout which is not updating. there are allot of post that mentioned that i need to used layout transform. Can any one please let me know what i am doing wrong or any other suggestion. <Slider x:Name="slider" Maximum="2" Minimum="0" Value="1" Width="100"/> </StackPanel> <ScrollViewer x:Name="scrollBar" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Grid.Column="1" Margin="6,1,1,1" Grid.Row="1"> <ScrollViewer.Content> <toolkit:LayoutTransformer Name="TheTransformer" Background="{x:Null}"> <toolkit:LayoutTransformer.LayoutTransform> <ScaleTransform x:Name="contentScale" ScaleX="{Binding ElementName=slider,Path=Value}" ScaleY="{Binding ElementName=slider,Path=Value}" /> </toolkit:LayoutTransformer.LayoutTransform> <toolkit:LayoutTransformer.Content> <Canvas x:Name="cnsImageEditable" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" OpacityMask="#FFE89E9E"> <!--The following commented bit does work i can zoomin and out but scroller not updating --> <!--<Canvas.RenderTransform> <ScaleTransform x:Name="scale" ScaleX="{Binding ElementName=slider,Path=Value}" ScaleY="{Binding ElementName=slider,Path=Value}"/> </Canvas.RenderTransform>--> <InkPresenter x:Name="inkCanvas" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <Image x:Name="imgEditableImage" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Image> </InkPresenter> </Canvas> </toolkit:LayoutTransformer.Content> </toolkit:LayoutTransformer> </ScrollViewer.Content> </ScrollViewer> any help is much appretiated thanks
Check out https://stackoverflow.com/questions/9899729/layouttransformer-in-silverlight-not-working , worked for me. Specifically: I found that Transforms in silverlight happens after Layout, so I changed the CanvasWidth property only once till the scrollbars show, and then zooming would work perfectly,
Microsoft Surface - Flip & Scatter View Items
I'm currently trying to get flip to work with scatterview items, and I'm having some trouble conceptually with it, using a plugin called Thriple (http://thriple.codeplex.com/). Essentially, a 2 sided thriple control looks like this: <thriple:ContentControl3D xmlns:thriple="http://thriple.codeplex.com/" Background="LightBlue" BorderBrush="Black" BorderThickness="2" MaxWidth="200" MaxHeight="200" > <thriple:ContentControl3D.Content> <Button Content="Front Side" Command="thriple:ContentControl3D.RotateCommand" Width="100" Height="100" /> </thriple:ContentControl3D.Content> <thriple:ContentControl3D.BackContent> <Button Content="Back Side" Command="thriple:ContentControl3D.RotateCommand" Width="100" Height="100" /> </thriple:ContentControl3D.BackContent> </thriple:ContentControl3D> What I'm struggling to grasp is if I should be making 2 separate ScatterView templates to bind to the data I want, and then each one would be the "front" and "back" of a scatterview item OR should i make 2 separate ScatterView items which are bound to the data I want, which is then bound to the "back" and "front" of a main ScatterView item? If there is a better way of using doing flip animations with ScatterViewItem's, that'd be cool too! Thanks!
I would create two separate templates for the data. To the user, it is still the same scatterviewitem (with two sides) so having them as two separate items makes little sense. You can specify which templates to use for front vs back in the properties of the ContentControl3D class. (They are of type DataTemplate) Code-wise, it'd look something like this: <thriple:ContentControl3D xmlns:thriple="http://thriple.codeplex.com/" Background="LightBlue" BorderBrush="Black" BorderThickness="2" MaxWidth="200" MaxHeight="200" Content="{Binding MyData}" BackContent="{Binding MyData}" ContentTemplate="{StaticResource MyFrontTemplate}" BackContentTemplate="{StaticResource MyBackTemplate}" /> You could also just specify the content directly in the declaration of the control (like you have your buttons above) if that makes more sense for you. That saves you from having to create data templates for the content: <thriple:ContentControl3D xmlns:thriple="http://thriple.codeplex.com/" Background="LightBlue" BorderBrush="Black" BorderThickness="2" MaxWidth="200" MaxHeight="200" > <thriple:ContentControl3D.Content> <Grid> <TextBlock Text="I'm the front" /> <TextBlock Text="{Binding SomeDataProperty}" /> <Button Content="Flip" Command="thriple:ContentControl3D.RotateCommand" Width="100" Height="100" /> </Grid> <thriple:ContentControl3D.BackContent> <Grid> <TextBlock Text="I'm the back" /> <TextBlock Text="{Binding SomeOtherDataProperty}" /> <Button Content="Flip" Command="thriple:ContentControl3D.RotateCommand" Width="100" Height="100" /> </Grid> </thriple:ContentControl3D.BackContent> </thriple:ContentControl3D>