Unable to reference/bind to view template in user control - wpf

I have a view template in a user control as below
<UserControl
x:Class="Configuration.Views.viewProfileTemplate"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid Background="Azure" Width="233">
<StackPanel Height="300" HorizontalAlignment="Left" Name="stackPanel1" VerticalAlignment="Top" Width="230">
<TextBox Text="{Binding Path=SomeProfileText}" Margin="10" />
<ListBox ItemsSource="{Binding Path=Profiles}" Height="70" Margin="10"/>
<CheckBox Content="CheckBox" Height="16" Name="checkBox1" Width="175" />
</StackPanel>
</Grid>
</UserControl>
The user control is defined as below in the main.xaml window
<Window x:Class="Configuration.TreeUI"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:views="clr-namespace:Configuration.Views"
xmlns:local="clr-namespace:Configuration"
Title="TreeUI" Height="300" Width="450" WindowStyle="ToolWindow">
I atempt to use the template with
<DataTemplate DataType="{x:Type local:ProfileViewModel}">
<Views:viewProfileTemplate/>
</DataTemplate>
But the compiler reports :
Error 1 The type 'Views:viewProfileTemplate' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.
I again am missing som important point, but what ?
Going to owe someone a big drink .....

There are two potential causes for this:
1) If the data template is in a different assembly than viewProfileTemplate, you need to make the xaml reflect this:
xmlns:views="clr-namespace:Configuration.Views;assembly=ViewAssembly"
Note that this will also require a reference to be added to the project properly to function.
2) If this is a design time error, make sure to build the solution first. The WPF designer in VS often will report errors about missing types until you've built once, then it "works" properly.

Related

Why does this Microsoft.VisualStudio.Imaging.CrispImage does not show anything?

I am trying to create a very simple WPF application to experiment this concept of the Visual Studio Image Service and Catalog.
I don't understand why I cannot seem to be able to show any of the known images (using KnownMonikers) both on the WPF designer or when the app runs.
The image is there. It just does not show anything.
Here's the WPF source code (following exactly the steps described in the MSDN link above):
<Window x:Class="ImageCatalogBrowser.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:ImageCatalogBrowser"
xmlns:imaging="clr-namespace:Microsoft.VisualStudio.Imaging;assembly=Microsoft.VisualStudio.Imaging"
xmlns:theming="clr-namespace:Microsoft.VisualStudio.PlatformUI;assembly=Microsoft.VisualStudio.Imaging"
xmlns:utilities="clr-namespace:Microsoft.VisualStudio.PlatformUI;assembly=Microsoft.VisualStudio.Utilities"
xmlns:catalog="clr-namespace:Microsoft.VisualStudio.Imaging;assembly=Microsoft.VisualStudio.ImageCatalog"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<utilities:BrushToColorConverter x:Key="BrushToColorConverter"/>
</Window.Resources>
<Grid>
<StackPanel
Background="White"
VerticalAlignment="Center"
theming:ImageThemingUtilities.ImageBackgroundColor="{Binding Background, RelativeSource={RelativeSource Self}, Converter={StaticResource BrushToColorConverter}}">
<Border BorderThickness="1" BorderBrush="Black" Width="33" Height="33">
<imaging:CrispImage
x:Name="crisp"
Visibility="Visible"
Width="32"
Height="32"
Moniker="{x:Static catalog:KnownMonikers.Save}" />
</Border>
</StackPanel>
</Grid>
</Window>
Can anyone help please?
What's your code-behind doing to initialize the ImageLibrary? If the library cannot find the moniker you're requesting, then it won't display the image.
Make sure when initializing the ImageLibrary you give it the path to the ImageCatalog's .imagemanifest file (in the VS install dir).

Don't understand why the user control we've written doesn't show up in a ModernWindow but does in a window

We're working on a WPF app, using the ModernUI for WPF. And we're also using MVVM Light. We've written some user controls which we intend to bring up in separate windows. Each of the windows are ModernUI's ModernWindow. Whenever we try to bring them up, nothing is shown in the ModernWindow. Out of frustration I thought I'd try doing the same thing in a plain old WPF window. Works perfectly each time. I don't know why it works in a regular WPF window but doesn't work in a ModernWindow. What is wrong???
Here's the XAML for the plain old window:
<Window x:Class="CoreFramework.BozoWindow"
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:view="clr-namespace:CoreFramework.View"
xmlns:local="clr-namespace:CoreFramework"
mc:Ignorable="d"
Title="Bozo Window" Height="300" Width="300">
<ContentControl Content="{Binding ProductList, Source={StaticResource Locator}}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
And here's the XAML for the ModernWindow:
<mui:ModernWindow x:Class="CoreFramework.DataViewWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
xmlns:vm="clr-namespace:CoreFramework.ViewModel"
xmlns:view="clr-namespace:CoreFramework.View"
xmlns:content="clr-namespace:CoreFramework"
Title="BOTS"
ShowInTaskbar="True"
Closing="ModernWindow_Closing">
<ContentControl Content="{Binding Path=ProductList, Source={StaticResource Locator}}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
One approach you could use is adding Menu links which is built into the Modern UI framework:
<mui:ModernWindow x:Class="ExampleApp.ExampleWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
Title="Scrum Manager" IsTitleVisible="True" ContentSource="/Views/DefaultUserControl.xaml"
x:Name="mainWindow" MenuLinkGroups="{Binding Groups}"
WindowStartupLocation="CenterScreen" WindowState="Maximized">
<i:Interaction.Triggers>
</i:Interaction.Triggers>
<mui:ModernWindow.TitleLinks>
<mui:Link DisplayName="settings" Source="/Views/SettingsWindow.xaml" />
<mui:Link DisplayName="Sign out" Source="/Infrastructure/Logout.xaml" />
</mui:ModernWindow.TitleLinks>
When a link is clicked, it opens up the 'Source' which would be UserControl xaml files.
See Github for more details:
https://github.com/firstfloorsoftware/mui

Add converter to page resources from code behind

I would like to add a specific instance of a class to the resources of a page then use that class as a converter, so in my page constructor I put:
this.Resources.Add("converterASD", new ASDConverter());
then bind to it like this:
<ListBox ItemsSource="{Binding Converter={StaticResource converterASD}}"/>
but I keep getting this exception:
Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an
exception.
I'm a bit new to WPF, any advice would be appreciated.
We could use more information from the exception.
Two suggestions:
Make sure that you add the resource before the call to InitializeComponent().
Try switching it to a dynamic resource.
You can declare the Converter you would like to use in the resource section of the page like the following example. (I recommend you declare the converter in XAML instead of the code-behind)
Example:
<UserControl x:Class="Views.ConverterExample"
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" mc:Ignorable="d" d:DesignHeight="300"
d:DesignWidth="300">
<UserControl.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter" />
</UserControl.Resources>
<Grid>
<CheckBox x:Name="VisibilityController" IsThreeState="False" />
<ListBox
Visibility="{Binding ElementName=VisibilityController, Path=IsChecked,Converter={StaticResource BoolToVisibilityConverter}}"
Height="100" Width="100" BorderBrush="Red" BorderThickness="1" />
</Grid>
</UserControl>

How can we bind selected row (or any property [for example Id column] from that row) to a property from ViewModel?

We are using MVVM Light Silverlight project for WCF Ria Service based project. After some research we decided to use DevExpress DataGrid with RiaInstantFeedbackDataSource. Everything works great until we wanted to bind grid's focused row to a ModelView property. Here is our XAML,
<UserControl x:Class="OurProject.Silverlight.Views.Personnel.List"
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:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" mc:Ignorable="d"
DataContext="{Binding Personnel, Source={StaticResource Locator}}"
d:DesignWidth="640" d:DesignHeight="480">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<dx:RiaInstantFeedbackDataSource x:Name="DataSource" QueryName="{Binding Path=QueryName}" KeyExpression="{Binding Path=KeyExpression}"
DomainContext="{Binding Path=Context}" />
<dxg:GridControl Name="grid" ItemsSource="{Binding ElementName=DataSource, Path=Data}" Grid.Row="0" AutoPopulateColumns="True" >
<dxg:GridControl.View>
<dxg:TableView x:Name="view" FocusedRow="{Binding Selected}" />
</dxg:GridControl.View>
</dxg:GridControl>
<Button Grid.Row="1" Height="30" VerticalAlignment="Bottom" Content="Edit" Command="{Binding Edit}" />
</Grid>
When we debug this code (breakpoint after Edit command executed) we realized that type of FocusedRow is object[]. We tried RowPropertyValueConverter but we can't find any document on how to use it.
How can we bind selected row (or any property [for example Id column] from that row) to a property from ViewModel?
P.S: We checked this link, it does not work for RiaInstantDataSource.
Thanks in advance.
OK, we solved this issue with
AreSourceRowsThreadSafe="True"
addition to our code.
<dx:RiaInstantFeedbackDataSource x:Name="DataSource" QueryName="{Binding Path=QueryName}" KeyExpression="{Binding Path=KeyExpression}" AreSourceRowsThreadSafe="True" DomainContext="{Binding Path=Context}" />
We will trace the impacts of this with multi-threading and will write here back any important foundings.

How can I display my user control in the MainWindow?

I'm trying to build a small MVVM test application, but can't really figure how to display my user control in the MainWindow.
My Solution Explorer:
I got a resource dictionary:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:MVVM.ViewModel"
xmlns:vw="clr-namespace:MVVM.View">
<DataTemplate DataType="{x:Type vm:ViewModel}">
<vw:View />
</DataTemplate>
</ResourceDictionary>
I got my view:
<UserControl x:Class="MVVM.View.View"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<DataTemplate x:Key="PersonTemplate">
<StackPanel>
<TextBlock Text="{Binding FirstName}" />
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<ListBox ItemsSource="{Binding Path=Persons}"
ItemTemplate="{StaticResource PersonTemplate}" />
</UserControl>
and My MainWindow
<Window x:Class="MVVM.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:MVVM.ViewModel"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary Source="MainWindowResources.xaml" />
</Window.Resources>
<Grid>
</Grid>
</Window>
The most obvious and easiest way is to add the ContentControl element:
<Grid>
<ContentControl x:Name="mainContentControl" />
</Grid>
And after that set the Content property of this control to your view model, and the corresponding view will be loaded and applied automatically:
this.mainContentControl.Content = new ViewModel.ViewModel();
But I would prefer to use another way without datatemplates:
<Grid>
<vw:View x:Name="mainView"/>
</Grid>
this.mainView.DataContext = new ViewModel.ViewModel();
Build your VS2010 solution, then, go to your MainWindow's XAML.
On the left, there is a toolbar with button "Toolbox"
Open it, it contains all the possible WPF controls you could add to your UI
Your UserControl should appear on top of the list (in a category probably named "MVVM Controls"), just drag&drop it to your UI :)

Resources