I have some problem on databinding to Image's Source Property.
I have a ListView Template
<Style x:Key="ListViewStyle" TargetType="{x:Type ListView}">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate x:Key="ListViewItemTemplate">
<StackPanel Width="60" Margin="5,5,10,5">
<Image Height="40" Width="40" Source="{Binding XPath=#Image}"
HorizontalAlignment="Center"/>
<TextBlock Text="{Binding XPath=#Name}" TextAlignment="Center"
TextWrapping="Wrap" FontWeight="bold"
HorizontalAlignment="Center" Margin="0,0,0,1" />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
This Style is defined in a Resource Dictionary File which is added in app.xaml. (So is accessible anywhere).
Now I have a window in which I have used this style for ListVew.
In WindowResources I have created an xmldatasource provider:
<XmlDataProvider x:Key="MyData" XPath="/Reports">
<x:XData>
<Reports xmlns="">
<Item Name = "Item1" Image="Resources\MenuIcons\Pic1.png"/>
<Item Name = "Item2" Image="Resources\MenuIcons\Pic2.png"/>
<Item Name = "Item3" Image="Resources\MenuIcons\Pic3.png"/>
</Reports>
</x:XData>
</XmlDataProvider>
and I have assigned this datasource as ItemsSource for Listview:
<ListView Name="MyListView" Style="{DynamicResource ListViewStyle}"
ItemTemplate="{StaticResource ListViewItemTemplate}"
ItemsSource="{Binding Source={StaticResource MyData},XPath=Item}"/>
My Problem is when I run application ListViewItem's Text is displaying, but Image is missing..
But When I defined this Style inside Window.Resource and used Style as StaticResource instead of Dynamic Resource, everything works fine.
Can Anybody tell me reason for not desplaying Image? I want to use this style globally(So I need to define it in ResourceDictionary). Can anyone help me?
I think nobody has answered your question yet because we couldn't see anything wrong.
I decided to try to reproduce your problem, but your code worked perfectly even though I tried to follow your repro steps.
Here are my steps:
Created a blank WPF Application
Added a ResouceDictionary file Dictionary1.xaml
Pasted your <Style> code into Dictionary1.xaml inside the <ResourceDictionary> tag
Edited out the typo where you set an x:Key on the DataTemplate
Added <Window.Resources> element to the top of Window1.xaml
Pasted your <XmlDataProvider> code into the <Window.Resources> tag
Replaced image pathnames, eg Image="Resources\MenuIcons\Pic1.jpg" became Image="TestImage.jpg"
Pasted your <ListView> code into the <Grid> tag
Removed your bogus ItemTemplate value, presuming this was left over from old code
Merged the dictionary "Dictionary1.xaml" into the Application's dictionary in App.xaml
Here is the relevant markup from App.xaml:
<Application
...>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
When I ran the application I saw a ListBox containing images with text underneath.
At this point I suggest you try reproducing my steps and see if your example works or not. If it does, try slowly changing things to be like your app that is not working. At some point you will discover what it is about your app that makes the difference. Then maybe we can give you some better advice how to fix it.
Hmm.. I found the problem... Problem was My Style Dictionary File and Images were in 2 separate directories in Project Directory. I saved All Dictionary Files in Styles Directory and all images in Resources Directory which is outside Styles Directory.
So I need to change Image path in xml as
<XmlDataProvider x:Key="MyData" XPath="/Reports">
<x:XData>
<Reports xmlns="">
<Item Name = "Item1" Image="..\Resources\MenuIcons\Pic1.png"/>
<Item Name = "Item2" Image="..\Resources\MenuIcons\Pic2.png"/>
<Item Name = "Item3" Image="..\Resources\MenuIcons\Pic3.png"/>
</Reports>
</x:XData>
</XmlDataProvider>
(I should Add ..\ before path) This solved my problem.. Thanks Ray Burns for your reply.. – Sasikumar D.R. 0
Related
I am fairly new to WPF. I understand the concept of defining global application resources to which I can refer throughout the application. I see I can define a textblock under application resources but can't seem to see how to refer to it within a window.
In the Application.Resources I have the following code:
<TextBlock x:Key="ABC_Copyright" Background="Beige" Text="Copyright 2016 ABC Company" />
How to I construct a new textblock in any given window that refers back to the "ABC_Copyright" application resource?
Thanks in advance.
We define it as a style;
<Style x:Key="ABC_Copyright" TargetType="TextBlock">
<Setter Property="Background" Value="Beige"/>
<Setter Property="Text" Value="Copyright 2016 ABC Company"/>
</Style>
Then we use it at whatever instance we need to;
<TextBlock Style="{StaticResource ABC_Copyright}"/>
Hope this helps, cheers.
Create a ResourceDictionary where you want to store and just put your your Resource Dictionary name as the source of ResourceDictionary in the App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Your Resource Dictionary Name"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Now in ResourceDictionarystyle your textbolckthat can be accessible from any other palces like that,
<Style x:Key="TxtStyle" TargetType="{x:Type TextBlock}">
<Setter Property="Text" Value="Copyright 2016 ABC Company"/>
</Style>
Now refer your textblock to the styled textblock
<TextBlock x:Key="ABC_Copyright" Background="Beige" Style="{StaticResource TxtStyle}"/>
I added custom named styles to the app.xaml.
I created an external resource dictionary (which I attach in the merged dictionaries of the app.xaml) and when I try to use one of the above named styles in the rcource dictionary, it says that there is no such style.
Also the default styles (i.e. unnamed styles that apply for the entire application) don't apply on the template elements.
Note: The Build Action of the templates is 'Page'.
Here is an example of how my code is written:
<Application x:Class="Application"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ShutdownMode="OnExplicitShutdown">
<Application.Resources>
<ResourceDictionary>
<Style
x:Key="StackPanelStyle"
TargetType="StackPanel"
BasedOn="{StaticResource {x:Type StackPanel}}">
<Setter Property="Margin" Value="5"/>
<Setter Property="Orientation" Value="Horizontal" />
<Setter Property="Height" Value="40"/>
</Style>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Templates/DataTemplate1.xaml"/>
<ResourceDictionary Source="/Templates/DataTemplate2.xaml"/>
<ResourceDictionary Source="/Templates/DataTemplate3.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
This an example of the data template:
<DataTemplate DataType="{x:Type Entity}" x:Key="NameDataTemplate">
<Expander>
<StackPanel>
<--The following line produces: StackPanelStyle was not found.-->
<StackPanel Style="{StaticResource StackPanelStyle}">
<Label Content="Name:"/>
<TextBox Text="{Binding Name}"/>
</StackPanel>
</StackPanel>
</Expander>
</DataTemplate>
Any ideas?
Do I have to merge the dictionaries in a different way?
The code won't work well because the DataTemplate in the resource dictionary doesn't know which one is using it, it just been used. Like Hollywood mode. They compiled separately.
To make this work, you can put your style which in app.xaml in the same resource dictionary of the DataTemplate or if you don't like this coupling, you can put it in a different resource dictionary, and merge it into the DataTemplate's resource dictionary.
The Build Action for your App.xaml should be ApplicationDefinition, and the build action for your Resource Dictionary files should be Page. I'm guessing you have both of those correct because they are default (and I see that you mentioned about Page already).
I can't think of any other problem with your situation. As long as your static resources are defined in the correct order, which they appear to be, it should be able to find them when you run your application.
Edit
Debugging idea: Create a fresh resource dictionary called "TestDictionary.xaml" with a simple button style. Make sure this dictionary is in the same folder as your other dictionaries (DataTemplate1.xaml, etc.). Put a link to only TestDictionary in MergedDictionaries (comment out the others). Put a button on your startup window and apply the style. See if just that works. If it fails, you know you have an issue with your merge. If it succeeds, something about your DataTemplate might be the problem.
How can I change the style -ONCE- for the scrollbars shown by all controls (listbox, treeview, scrollbarviewer, richtextbox, etc...)?
If you will define your Style for a control with no x:Key attribute, it will be applied for all instances of that control.
Try like this:
<Window.Resources>
<Style TargetType="ScrollBar">
<Setter Property="Background" Value="Red"/>
</Style>
</Window.Resources>
<Grid>
<TextBox Margin="24,12,0,0" VerticalScrollBarVisibility="Visible" AcceptsReturn="True" Height="28" VerticalAlignment="Top" HorizontalAlignment="Left" Width="89" />
<ScrollBar Name="scroll" HorizontalAlignment="Right" />
</Grid>
Here you can see that the Style is defined for ScrollBar control and have no x:Key attribute defined so it gets applied to the each ScrollBar instance within Window. Like ScrollBar of TextBox and ScrollBar named scroll also.
Hope this helps!
Thanks. Finally the problem was solved by setting the style in the Themes/Generic.xaml and (because of custom controls existing in another assembly with they respective resources merged) adding the following to App.xaml...
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/Generic.xaml"/>
<ResourceDictionary Source="MyCtls/MyRes.xaml" />
</ResourceDictionary>
</Application.Resources>
The key point is to merge the Generic.xaml file.
Also, if the resource dictionary is in another assembly, it must be referenced as...
<ResourceDictionary Source="pack://application:,,,/OtherAssembly;component/MyCtls/MyRes.xaml"/>
I have a style for a textblock that is set inside my app.xaml this is then applied to textblocked through out my application and works fine.
However i get an error: "could not create instance of type" if i apply this style to a textblock within my user control, how come this is a problem?
<UserControl x:Class="Client.Usercontrols.MyButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" MinHeight="30" MinWidth="40"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Button Width="Auto" HorizontalAlignment="Center">
<Border CornerRadius="5" BorderThickness="1" BorderBrush="Transparent" >
<Grid>
<Image Name="tehImage" Source="{Binding ImageSource}" />
<TextBlock Name="tehText" Text="{Binding Text}"
Style="{StaticResource ButtonText}" /> <-- This causes error
</Grid>
</Border>
</Button>
Thanks,
Kohan
-- App.Xaml Code --
<Application x:Class="Client.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Mainpage.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/CascadingStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
-- CascadingStyles.Xaml --
<Style TargetType="{x:Type TextBlock}" x:Key="ButtonText" >
<Setter Property="FontSize" Value="10" />
<Setter Property="VerticalAlignment" Value="Bottom" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="FontFamily" Value="Lucida Sans Unicode" />
<Setter Property="Foreground" Value="#0F004E" />
</Style>
Basically, it can not find the StaticResource because it is not in the file with your user control. UserControl.xaml knows nothing about App.xaml.
You should use DynamicResource instead, this way it will be applied at runtime.
The previous answer is absolutely incorrect. You can definitely define resources at the application level and reference them from within UserControls. In fact, that can often increase performance to prevent resource duplication. Application resources are checked 3rd in the list for Static Resources as described on this page under the heading "Static resource lookup behavior".
I'm guessing you have a typo or some other problem causing your error. Could you post the app.xaml code?
I have lost some hours on such a problem, but it only applies to Expression Blend 4.
As explained in this blog post:
http://blogs.msdn.com/b/unnir/archive/2009/03/31/blend-wpf-and-resource-references.aspx
Expression will try to resolve StaticResources using the Blend Application.Resources instead of your application Application.Resources. This seems to happen still on Blend 4.0.30422.0
Ok, I was programming an app that loaded merged dictionaries on runtime to change appearance and behaviour when I got stuck : some of the controls on my forms just weren't reacting to the styles I thought they had to react to.
I have tried to simplify the problem as much as I could and came up with something so simple that I'm afraid I am overlooking the bleeding obvious, but anyway here it goes :
<Window x:Class="Example.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="100" Width="50">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="Green"></Setter>
</Style>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<StackPanel>
<TextBox Text="1" Name="box1"/>
<TextBox Text="2" Name="box2"/>
<TextBox Text="3" Name="box3"/>
</StackPanel>
</Window>
The question is : why is the first textbox not green?
==> that is, the designer shows it in green, but when running the app, it is no longer...
I know the solotion to this particular problem is to remove the merged dicitonary tags, but I need to know how to solve this using merged dictionaries.
Thanks!
MergedDictionaries have always been quirky, you can set any resources you want in them, but they only process outside resource dictionary references at runtime.
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="StylesDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
The only requirement is that the resources be set in an other dictionary.
Try this...
<Style TargetType="{x:Type TextBox}">
I'm not sure if this will work, but when I was having styling problems, I fixed them by using
TargetType="{x:Type TextBox}"