Image Source Property - Inside DataTemplate and UserControl.Resources - Image from different assembly - wpf

Using the Image Source Property just works, if I'm not inside a DataTemplate.
Otherwise he cannot find the picture which is in another assembly named "Images".
XAML, that works. I can see the Image, holded by the "Images" assembly:
<UserControl x:Class="Views.ViewUserInfo"
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="600">
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Horizontal" Margin="0,5,0,5">
<TextBlock Text="Authorized: "/>
<TextBlock Text="{Binding Path=IsAuthorized, Mode=OneWay}" VerticalAlignment="Center"/>
<Image Width="16" Height="16" Source="/Images;Component/Img16/Ok.png" />
</StackPanel>
</StackPanel>
</UserControl>
Does not work:
<UserControl x:Class="Views.ViewUserInfo"
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="600">
<UserControl.Resources>
<DataTemplate DataType="{x:Type System:Boolean}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="DataTemplate has been found " />
<Image Width="16" Height="16" Source="/Images;Component/Img16/Ok.png" />
</StackPanel>
<DataTemplate.Resources>
<!--simplyfied, Triggers removed...--->
</DataTemplate.Resources>
</DataTemplate>
</UserControl.Resources>
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Horizontal" Margin="0,5,0,5">
<TextBlock Text="Authorized: "/>
<ContentPresenter Content="{Binding Path=IsAuthorized, Mode=OneWay}" VerticalAlignment="Center"/>
<!--IsAuthorized.GetType() = typeof(System.Boolean)-->
</StackPanel>
</StackPanel>
</UserControl>
He's actually in the DataTemplate, because he shows me the Text "DataTemplate has been found" but i cannot see any picture..
Whats the problem here?

You said you see the text box, how can that be, your template is empty, your StackPanel is just a resource in your DataTemplate. Try removing the <DataTemplate.Resources> and </DataTemplate.Resources> lines or maybe add your <!--simplyfied, Triggers removed...--->.

Dont use ContentPresenter. Use ContentControl.
<ContentControl Content="{Binding Path=IsAuthorized, Mode=OneWay}"
VerticalAlignment="Center"/>

Related

'Type Not Defined' Error After Adding x:Name Attribute To User Control Declaration Inside A User Control

I am having trouble with the Xaml passer throwing a 'Type Not Defined' exception after I add a x:Name attribute to a user control declaration which is inside another user control.
<UserControl x:Class="LoginView"
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:vm="clr-namespace:PowderKegSoftware.Artisan.Client.Authentication"
xmlns:ctrl="clr-namespace:PowderKegSoftware.Artisan.Client.Ui.Controls;assembly=CommunalUi"
xmlns:auth="clr-namespace:PowderKegSoftware.Artisan.Client.Authentication"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=vm:LoginViewmodel,
IsDesignTimeCreatable=False}"
d:DesignHeight="693" d:DesignWidth="1080">
<UserControl.Resources>
---------------------
</UserControl.Resources>
------------------------
<Border BorderThickness="0">
<Border.Background>
<ImageBrush ImageSource="{Binding BackgroundImage}"
Stretch="None" />
<TextBlock Grid.Column="0"
Grid.Row="1"
Text="{Binding UsernameLabelText}"
Style="{StaticResource TextBlockBaseStyle}"/>
<ctrl:ArtisanComboBox Grid.Column="0"
Grid.Row="2"
Height="30"
TabIndex="10"
Style="{StaticResource ArtisanComboBoxDefaultStyle}"
DataContext="{Binding UsernamesVm}" />
<TextBlock Grid.Column="0"
Grid.Row="3"
Text="{Binding PasswordLabelText}"
Style="{StaticResource TextBlockBaseStyle}"
Margin="0 20 0 0"/>
------------------
This all works just fine until I try and add the x:Name attribute to the <ctrl:ArtisanComboBox ... declaration.
<ctrl:ArtisanComboBox Grid.Column="0"
Grid.Row="2"
x:Name="ChoosableUserNames" <!-- Throws a 'Type Not Defined' error. -->
Height="30"
TabIndex="10"
Style="{StaticResource ArtisanComboBoxDefaultStyle}"
DataContext="{Binding UsernamesVm}" />
I have tried using x:Name= and just Name= but neither complies. I want to name this declaration so I can use FocusManager.FocusedElement="{Binding ElementName=ChoosableUserNames} to set the focus on this control when the view is renered.
Thanks for any help you can give.

WPF Combobox SelectionChanged Event is not firing

I have a comboBox, user controls and a button. SelectionChanged event is not firing in my UI App but when I copy the same combobox code in a different WPF app, selectionChanged event is firing. I don't understand why event is not triggered just here.
<Window x:Class="ExecutionInformation.TesterApp.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:ExecutionInformation.TesterApp.UserControls"
mc:Ignorable="d"
Title="MainWindow" Height="Auto" Width="800">
<ScrollViewer>
<Grid>
<StackPanel>
<Label Content="Execution Information service" Margin="20,10,20,10"
HorizontalAlignment="Center" FontSize="15" FontFamily="Bold"/>
</StackPanel>
<StackPanel Margin="20">
<!--<ComboBox Name="CmbAPIList" ItemsSource="{StaticResource ExecInfoServiceApis}"
DisplayMemberPath ="ValueString"
SelectedValuePath="ValueString" Margin="100,20,100,0"
SelectedValue="{Binding ColorString}" SelectionChanged="CmbAPIList_SelectionChanged"/>-->
<ComboBox Name="CmbAPIList"
SelectionChanged="CmbAPIList_SelectionChanged">
<ComboBoxItem>ApproveTestStartAsync</ComboBoxItem>
<ComboBoxItem>CompleteTestAsync</ComboBoxItem>
<ComboBoxItem>GetPartAsync</ComboBoxItem>
<ComboBoxItem>GetAssemblyAsync</ComboBoxItem>
<ComboBoxItem>PutAssemblyAttributesAsync</ComboBoxItem>
</ComboBox>
<local:ApproveTestStartUC x:Name="approveTestStartUC" Visibility="Collapsed" Margin="20,10,20,10" Background="Cyan" />
<local:CompleteTestUC x:Name="completeTestUC" Visibility="Collapsed" Margin="20,10,20,10" />
<local:GetPartUC x:Name="getPartUC" Visibility="Collapsed" Margin="20,10,20,10" />
<local:GetAssembly x:Name="getAssemblyUC" Visibility="Collapsed" Margin="20,10,20,10" />
<local:PutAssemblyAttributesUC x:Name="putAssemblyUC" Visibility="Collapsed" Margin="20,10,20,10"/>
<WrapPanel Margin="15" HorizontalAlignment="Center">
<Button Name="btnShowResults" Click="btnShowResults_Click" Visibility="Collapsed" Width="100">Show Results</Button>
</WrapPanel>
<Border BorderThickness="1" BorderBrush="LightGreen">
<TextBlock Name="TxtResults" Visibility="Collapsed"></TextBlock>
</Border>
</StackPanel>
</Grid>
</ScrollViewer>
Commented ComboBox code in the above code works without any issues.
Thanks

How to add images to UserControl menu in Wpf

enter code here<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"
xmlns:local="clr-namespace:EPOS.Desktop.View"
xmlns:vm="clr-namespace:EPOS.Desktop.ViewModel"
xmlns:UserControls="clr-namespace:EPOS.Desktop.UserControls"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" xmlns:dxwui="http://schemas.devexpress.com/winfx/2008/xaml/windowsui" xmlns:dxn="http://schemas.devexpress.com/winfx/2008/xaml/navbar" x:Class="EPOS.Desktop.View.MainSaleUI"
mc:Ignorable="d"
Title="MainSaleUI"
Width="1046" Height="500" Left="500" Top="500"
Background="SkyBlue"
WindowStartupLocation="CenterScreen"
>
<Window.Resources>
<DataTemplate DataType="{x:Type vm:QeueOrdersViewViewModel}">
<UserControls:QeueOrders />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:TillViewModel}">
<UserControls:TillUC/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:SettingViewModel}">
<UserControls:Settings/>
</DataTemplate>
</Window.Resources>
<Grid Margin="0,67,65,0">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<ListBox x:Name="ListBoxMenu"
Grid.Column="0" Margin="5"
ItemsSource="{Binding Settings}"
SelectedIndex="0">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" Padding="10"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Border Grid.Column="1" Margin="5,5,10,5" BorderBrush="#FF7F9DB9" BorderThickness="1">
<ContentControl Content="{Binding ElementName=ListBoxMenu, Path=SelectedItem}" Margin="0,0,225,0"/>
</Border>
</Grid>
Blockquote
How to add images to User-control menu in Wpf. These are dynamic menus. Is there way to add images to every text?
I'm not sure about what you exactly want... assuming that you want to display some image right after /before the text on your listbox you can add an image property in your "settings" class (Binding of your Listbox's Itemsource) and change your listbox's datatemplate content with something like this :
<StackPanel>
<TextBlock Text="{Binding Name}" Padding="10"/>
<Image Source="{Binding Image }" />
</StackPanel>

Overriding Style of WPF TreeView Items

I am using the awesome MahAppsMetro WPF control suite. I have a tree view that loads the file system. I also want to display images in the TreeViewItems so I have overridden the metro tree style as follows
<UserControl x:Class="GDE.Tree.TreeView"
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:Caliburn="http://www.caliburnproject.org"
xmlns:Metro="http://metro.mahapps.com/winfx/xaml/controls"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="300">
<UserControl.Resources>
<HierarchicalDataTemplate x:Key="TreeTemplate" ItemsSource="{Binding Path=Children}"/>
</UserControl.Resources>
<!--TreeView-->
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBox Grid.Row="0"
Margin="5"
IsReadOnly="True"
Text="{Binding SelectedPath, Mode=TwoWay}"/>
<TreeView Grid.Row="1"
Margin="5"
ItemsSource="{Binding RootChildren}"
ItemTemplate="{StaticResource TreeTemplate}">
<TreeView.Resources>
<Style TargetType="{x:Type TreeViewItem}"
BasedOn="{StaticResource MetroTreeViewItem}">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Name="img"
Width="16"
Height="16"
Stretch="Fill"
Source="{Binding Path=Icon, Mode=OneTime}"
VerticalAlignment="Center"
HorizontalAlignment="Center"/>
<TextBlock Text="{Binding DisplayName, Mode=OneTime}"
Margin="5,0,0,0"
VerticalAlignment="Center"
HorizontalAlignment="Left"/>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</TreeView.Resources>
</TreeView>
</Grid>
</UserControl>
But this does not seem to work as intended and I can't figure out why. From this XAML, I get no binding errors and the visuals look like:
I have gone more extensive with the XAML mark up but I am getting the exact same visuals. How can I change the above XAML to give me the Image/TextBlocks as well as the MahApps look and feel?
Thanks for your time.
To sum up comments instead of changing Style you need to move StackPanel from DataTemplate directly into HierarchicalDataTemplate as at the moment used template has no content:
<HierarchicalDataTemplate x:Key="TreeTemplate" ItemsSource="{Binding Path=Children}">
<StackPanel Orientation="Horizontal">
<Image Name="img" ... />
<TextBlock Text="{Binding DisplayName, Mode=OneTime}" ... />
</StackPanel>
</HierarchicalDataTemplate>

custom controls and

hey there,
i have written a custom control for my App so things get a little bit easier for me,and so far it worked great , now i wanted to bind some data to the wrapped content but Output says that i have a binding error and my "Items" property is searched at "CLIENT.UI.SinglePageControl" instead of "CLIENT.MainPage"....
<phone:PhoneApplicationPage
x:Class="CLIENT.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="clr-namespace:CLIENT.UI"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="PortraitOrLandscape" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<Grid>
<ui:SinglePageControl HeaderTitle="Connections">
<ui:SinglePageControl.PageContent>
<ListBox x:Name="MainListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}" Background="Blue" SelectionChanged="MainListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Orientation="Horizontal">
<Image Source="UI/PICS/list_connection.png"/>
<TextBlock Text="{Binding ItemText}" TextWrapping="Wrap" Foreground="Black"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ui:SinglePageControl.PageContent>
</ui:SinglePageControl>
</Grid>
Try giving your control an x:Name value and then in your binding statement include ElementName=<x:Name>
<phone:PhoneApplicationPage
x:Name="pa"
x:Class="CLIENT.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="clr-namespace:CLIENT.UI"
mc:Ignorable="d"
d:DesignWidth="480"
d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="PortraitOrLandscape"
Orientation="Portrait" shell:SystemTray.IsVisible="True">
<Grid>
<ui:SinglePageControl HeaderTitle="Connections">
<ui:SinglePageControl.PageContent>
<ListBox x:Name="MainListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items, ElementName=pa}" Background="Blue" SelectionChanged="MainListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Orientation="Horizontal">
<Image Source="UI/PICS/list_connection.png"/> <TextBlock Text="{Binding ItemText, ElementName=pa}" TextWrapping="Wrap" Foreground="Black"/>
</StackPanel> </DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ui:SinglePageControl.PageContent>
</ui:SinglePageControl>
</Grid>
Without seeing how you're setting data contexts, etc it's difficult to tell, but judging by the details you've given, at the level of your ListBox your data context is the SinglePageControl.PageContent. Ordinarily, the data context of the parent (the MainPage) would be inherited down the visual tree, so the fact that it's not in this case implies that SinglePageControl.PageContent is setting it's own data context. If you don't need it it, then simply remove the code (this.DataContext = this; for example) that is setting it and the data context will then be inherited.
If you have good reason for setting a data context at the page content (which would seem perfectly reasonable), then you will need to provide a way of passing that information down, but we'll need to know a bit more about what data comes from where in order to give a good solution.

Resources