Can I use a resource declared in its own XXX.Resources? - wpf

I've a Window, in which I declare one Resource, I was hopping to be able to use it in the window itself, but I'm not sure this is possible?
<dx:ThemedWindow x:Class="SomeWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:dxco="http://schemas.devexpress.com/winfx/2008/xaml/controls"
xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:frameworkExtensions="clr-namespace:SomeNamespace.FrameworkExtensions"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" ContentTemplate="{StaticResource dataTemplate}" >
<dx:ThemedWindow.Resources>
<frameworkExtensions:ConventionBasedDataTemplateSelector x:Key="dataTemplate"/>
</dx:ThemedWindow.Resources>
[...]
</dx:ThemedWindow>
Here by example it's for a custom DataTemplate.
How should I do this? Because currently Visual Studio is warning me that it can't find "dataTemplate"

Your problem is because you're trying to use a resource before it's there.
Let's consider a simple example.
If you did:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="400"
>
<Window.Resources>
<SolidColorBrush x:Key="TestResourceBrush" Color="Blue"/>
</Window.Resources>
<Window.Background>
<StaticResource ResourceKey="TestResourceBrush"/>
</Window.Background>
<Grid>
</Grid>
</Window>
Then you see no error and the background of the window turns blue.
If you instead do:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="400"
Background="{StaticResource TestResourceBrush}"
>
<Window.Resources>
<SolidColorBrush x:Key="TestResourceBrush" Color="Blue"/>
</Window.Resources>
<Grid>
</Grid>
</Window>
Then that will error, saying it can't resolve the resource.
If you made that a DynamicResource, then that works ok.
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="400"
Background="{DynamicResource TestResourceBrush}"
>
<Window.Resources>
<SolidColorBrush x:Key="TestResourceBrush" Color="Blue"/>
</Window.Resources>
<Grid>
</Grid>
</Window>

The way resources work is that the control looks for the resource in itsself, then in the parent then all the way up the visual tree until it hits the application. see here
so if you had the resource declared on a listview then the panel it is contained within can't use the resource but the list view can.
i suspect the issue you're having is that the template doesn't exist yet because you haven't compiled your code. because wpf is so flexible the designer actually has to run the code to see how it works so if your code hasn't been compiled or has changed since the last compilation the designer can't see the changes.
failing that it may just be order of execution, ie your consuming your template in an attribute but creating it in a child element, in this case move the contentTemplate to an element below the resources element and you should have access to it.
ie
<dx:ThemedWindow.Resources>
<frameworkExtensions:ConventionBasedDataTemplateSelector x:Key="dataTemplate"/>
</dx:ThemedWindow.Resources>
<dx:ThemedWindow.ContentTemplate>
<StaticResource ResourceKey="dataTemplate"/>
</dx:ThemedWindow.ContentTemplate>

Related

Attaching CollectionViewType to a CollectionViewSource

This might be simple but I do not know how to go about it. I want to attach a CollectionViewType like this: CollectionViewType="ListCollectionView" to a CollectionViewSource. I am using a typed DataSet and the CollectionViewSource was generated through a Drag and Drop. I am using a DataSet and not Entity Framework.
I have put the following references:
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
However, I am failing to modify the CollectionViewSorce accordingly.
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow"
Width="500"
Height="400"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
>
<Window.Resources>
<local:DataSet1 x:Key="DataSet1" />
<CollectionViewSource x:Key="ExpenseDescriptionsViewSource" Source="{Binding ExpenseDescriptions, Source={StaticResource DataSet1}}" />
</Window.Resources>
<Grid >
</Grid>
</Window>
All the examples I have seen deal with Entity Framework and not with DataSet.
How can I implement CollectionViewType changes in the CollectionViewSource above using DataSset?

WPF Adding UserControl to RadRibbonTab

I'm learning to use WPF.
I'm trying to make RadRibonTab in one file and it's content in the other, however i'm unable to do that.
My parent UserControl:
<UserControl x:Class="TelerikTotorial2.RibbonBar.RibonBarView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:historyTab="clr-namespace:TelerikTotorial2.RibbonBar.History"
mc:Ignorable="d"
>
<telerik:RadRichTextBoxRibbonUI ApplicationButtonContent="File" CollapseThresholdSize="50,50" VerticalAlignment="Top" Margin="0,0,0,-0.2">
<telerik:RadRibbonTab Header="History" HorizontalAlignment="Left" Margin="0" Width="107.2">
<historyTab:HistoryTabView/>
</telerik:RadRibbonTab>
</telerik:RadRichTextBoxRibbonUI>
</UserControl>
UserControl which I try to add
<UserControl x:Class="TelerikTotorial2.RibbonBar.History.HistoryTabView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:history="clr-namespace:TelerikTotorial2.RibbonBar.History"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<telerik:RadRibbonGroup Header="Tools">
<telerik:RadRibbonButton Text="View" Size="Large"/>
</telerik:RadRibbonGroup></UserControl>
However I get exception "InvalidOperationException: Specified element is already the logical child of another element.".
If i replace <historyTab:HistoryTabView/> with
<telerik:RadRibbonGroup Header="Tools">
<telerik:RadRibbonButton Text="View" Size="Large"/>
</telerik:RadRibbonGroup>
then everything is working, however I want to have this code in separate files. Perhaps someone can explain what I am doing wrong? Perhaps it isn't possible to add UserControl to other UserControl?

WPF Error: Do not recognize or can not access the member "resource"

Ok, I have to say that I just started using WPF and I know nothing about XAML, so for my first project with WPF, I'll be trying to build my own app for my own buisness, But im getting a problem trying to get an image resource.
This is what I tried
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="605.027" Width="1042.214" WindowStyle="None" Background="{StaticResource Circuits}">
<Application.resource>
<ImageBrush x:Key="Circuits" ImageSource="Circuits.jpg"/>
</Application.resource>
</Window>`
But at the <Application.resource> line I'm getting the error
Do not recognize or can not access the member "resource"
and I just dont know what to do
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="605.027" Width="1042.214" WindowStyle="None" Background="{StaticResource Circuits}">
<Window.Resources>
<ImageBrush x:Key="Circuits" ImageSource="Circuits.jpg"/>
</Window.Resources>
</Window>`
Have you tried :
<Application.Resources>
So in your exemple :
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="605.027" Width="1042.214" WindowStyle="None" Background=" {StaticResource Circuits}">
<Application.Resources>
<ImageBrush x:Key="Circuits" ImageSource="Circuits.jpg"/>
</Application.Resources>
</Window>

Align WPF window on startup

I have a WPF window which is positioned incorrectly when shown.
I want it to be positioned correctly, like this:
How may I accomplish this?
This is the xaml for the window:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="WpfApplication9.MainWindow"
Title="MainWindow" Height="1050" Width="910" Background="{x:Null}" >
There's a very handy XAML tag called WindowStartupLocation that allows you to specify where it starts. It is used like this:
<Window
...
Title="MainWindow"
Height="1050"
Width="910"
Background="{x:Null}"
WindowStartupLocation="CenterScreen">
This will start it in the center of the window where the mouse pointer is.
Edit: There is also an option called CenterOwner if you want it to always be the same monitor you start your app on.
Try the following (it will bind the top of your window to the top of the screen):
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="WpfApplication9.MainWindow"
Title="MainWindow" Height="1050" Width="910" Background="{x:Null}" WindowStartupLocation="Manual" Top="0">

How to add designmode dummy data into WPF control

Added entity Dummy to the usercontrol resources but Visual Studio complains it can't find resource 'Dummy'. Is it possible to add design data this way? What am I doing wrong?
<UserControl x:Class="MovieScraper.Media"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:entity="clr-namespace:Processor.Entity;assembly=Processor"
mc:Ignorable="d"
d:DataContext="{StaticResource ResourceKey=Dummy}"
d:DesignHeight="300" d:DesignWidth="300" Background="White">
<UserControl.Resources>
<entity:Media x:Key="Dummy" Title="Akira"></entity:Media>
</UserControl.Resources>
<Grid>
<TextBlock Text="{Binding Title}"></TextBlock>
</Grid>
</UserControl>
I would create a MockDataContext model. This works pretty nice. This is a snippet from code that I have:
<UserControl x:Class="Modules.Core.Views.HeaderView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:viewModels="clr-namespace:Modules.Core.ViewModels"
mc:Ignorable="d"
d:DesignHeight="100" d:DesignWidth="1024">
<Grid d:DataContext="{d:DesignInstance Type=viewModels:MockHeaderViewModel, IsDesignTimeCreatable=True}">
</Grid>
</UserControl>
Big advantage is that you can actually run some code. For example I use it to update the time in my header in design time and vary some fields. You can immediately see if your binding work and your layout is giving you components enough space.
You are trying to bind d:DataContext to resource Dummy, but you are missing the keyword Binding. Please change this line to
d:DataContext="{Binding Source={StaticResource Dummy}}

Resources