I have all of my images stored in a single folder in my wpf project, is there a way to reference the source path without having to copy and paste it all over the project?
Currently I have the following working
<Trigger Property="IsMouseOver" Value="False">
<Setter TargetName="BtnImage" Property="Source" Value="pack://application:,,,/Project;component/Images/BlueGear.png"/>
<Setter TargetName="RefreshGrid" Property="Background" Value="#FAFAFAFA" />
<Setter TargetName="BtnText" Property="Foreground" Value="Gray" />
</Trigger>
I would eventually like to just reference the image path of
pack://application:,,,/Project;component/Images/
to something like
Source=Images/BlueGear.png
Can this be done?
Try change Build Action in Properties of your image in solution to Resource.
Related
This error only appear when I added <ImageSource x:key="...">image resource file</ImageSource> into my <application.resources> section. However the program working fine in runtime without any problem.
Before (original code => 0 error):
<Application.Resources>
<FontFamily x:Key="NotoBold">
fonts/NotoSerifCJKtc-Bold.otf#Noto Serif CJK TC Bold
</FontFamily>
<Style x:Key="HeaderTitle" TargetType="TextBlock">
<Setter Property="Grid.Column" Value="0" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="FontSize" Value="18" />
<Setter Property="FontFamily" Value="{StaticResource NotoBold}" />
<Setter Property="Foreground" Value="White" />
</Style>
... ...
... ...
... ...
</Application.Resources>
After (added ImageSource xkey='RedCircle1' => 1 error):
<Application.Resources>
<FontFamily x:Key="NotoBold">
fonts/NotoSerifCJKtc-Bold.otf#Noto Serif CJK TC Bold
</FontFamily>
<ImageSource x:Key="RedCircle1">
images/sample/circleRed1.png
</ImageSource>
<Style x:Key="HeaderTitle" TargetType="TextBlock">
<Setter Property="Grid.Column" Value="0" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="FontSize" Value="18" />
<Setter Property="FontFamily" Value="{StaticResource NotoBold}" />
<Setter Property="Foreground" Value="White" />
</Style>
... ...
... ...
... ...
</Application.Resources>
The error hint appear on <Style x:Key="HeaderTitle" ... >. If I change the setter value="{StaticResource NotoBold}" to something else(e.g. value="Verdana") the error will gone.
The error message:
MarkupExtension is not valid for Setter.Value. The only supported MarkupExtension types are DynamicResourceExtension and BindingBase or derived types.
UPDATE
My .Net version is 4.8.03752, Visual Studio 2019 version is 16.8.3. All my resource files(Noto font and circleRed1.png) their build actions are "Resources".
Step to re-produce the problem.
create a new WPF .NET project.
include the resources(font & image) into project and build them as "Resources".
copy the source code into <application.resources> section.
(Until this step everything still fine no any error message appear.)
Press "Run" to execute your program, a blank window will appear(we still haven't code xaml), then press "Stop" to exit the debugging/runtime.
Now the error message appear.
copy the source code into xaml page and run the program again.
Although there are some errors but the program working fine in runtime and all the resources(font & image) are display correctly.
screenshot of the error
program runtime & error on xaml page
My temporary solution:
Remove one character of my StaticResource variable name, "NotoBold" => "otoBold"
Now the error message become Can't recognized "otoBold" ...
Undo the edit, "otoBold" => "NotoBold"
All error message gone. The XAML designer also display the Noto font correctly.
temporary solution
xaml designer view after solution fix
Now everytime after the debugging, I have to fix everything again, for else my xaml designer view won't display correctly(some controls their style attribute won't take effect).
Please try to modify the code like the following:
<Setter Property="FontFamily" Value="{DynamicResource NotoBold}" />
A StaticResource will be resolved and assigned to the property during the loading of the XAML which occurs before the application is actually run. It will only be assigned once and any changes to resource dictionary ignored.
A DynamicResource assigns an Expression object to the property during loading but does not actually lookup the resource until runtime when the Expression object is asked for the value. This defers looking up the resource until it is needed at runtime.
You could analyze whether to use DynamicResource or StaticResource according to the specific situation
I meet a problem with Fluent Ribbon. I didn't change anything in XAML, but all Headers of my buttons disapeared. I don't know if it is a bug, or simply a shortcut to change appearance and Hide/Show headers.
Here is how looks the ribbon :
And how it looks like from now :
I tried to restart solution but nothing changed, same if I restart VisualStudio. But if I load a backup of solution last week all is working fine, that's why I think there is maybe a shortcut to make them appear/disappear?
Edit :
After additional tests I can add :
Issue is present in both Debug and Release mode
Problem is on all the solution, in all windows using Ribbon, not only that one
If the code is OK, close the project and clear the cache. After that do not forget to delete the folders "bin" and "debug" and "release". Open the project, do a rebuild. Hope to help.
I finally decided to restart from a backup, and modify again the code file after file to finally find the issue (apologies to whom I said that I didn't modify xaml, didn't remember about that one, and didn't notice inmediately that this caused a problem).
In my App.xaml file I use the following style :
<Style TargetType="ToolTip">
<Style.Triggers>
<Trigger Property="Content"
Value="{x:Static sys:String.Empty}">
<Setter Property="Visibility"
Value="Collapsed" />
</Trigger>
<Trigger Property="Content"
Value="{x:Null}">
<Setter Property="Visibility"
Value="Collapsed" />
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="Content"
Value="{x:Static sys:String.Empty}">
<Setter Property="Visibility"
Value="Collapsed" />
</Trigger>
<Trigger Property="Content"
Value="{x:Null}">
<Setter Property="Visibility"
Value="Collapsed" />
</Trigger>
</Style.Triggers>
I had to remove all the block and all is working again.
But then I don't understand why Ribbon headers are influented by the style? Well I guess the headers are in textboxes, but they are not empty?
I want to give mij text on my button a customized color in the MainWindow.xaml.cs
Normally you give the color in the cs file by this way to the command:
ToggleButton.Foreground = Brushes.Green;
But I want to give the hexnumber
I've already tried something like this :
SolidColorBrush Owncolor = (SolidColorBrush)(new BrushConverter().ConvertFrom("#FF5D0000"));
ToggleButton.Foreground = Brushes.Owncolor;
Instead of doing it in code behind (unless you have a very specific reason to do that), you can work on your xaml
<ToggleButton Foreground = "#FF5D0000"/>
if you are doing it based on some condition, also please take a look at this. It's always a better practice to handle graphical stuff in your xaml as much as you can
for instance you can do
<ToggleButton>
<ToggleButton.Style>
<Style TargetType="{x:Type ToggleButton}">
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Foreground" Value="Green"/>
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>
Usually in WPF you tend to use styles defined in XAML to change how controls looks. However, using the BrushConverter works if you absolutely have to use the hexadecimal syntax in codebehind. I'd consider building a new SolidColorBrush with Color.FromArgb easier, but that also works.
As for how to use styles and XAML properly, you should probably read some tutorials or books. WPF is quite a different beast than Windows Forms or a lot of older UI frameworks, so there's some re-learning required.
The simplest way of achieving what you want (a different text colour when the button is pressed) would be the following style:
<Style TargetType="ToggleButton">
<Setter Property="Foreground" Value="#FF5D0000"/>
<Style.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Foreground" Value="Green"/>
</Trigger>
</Style.Triggers>
</Style>
When placed in the Resources of your Window it would apply to all ToggleButtons in that window.
I am using silverlight and cant edit the header style of the data grid.
<sdk:DataGridTemplateColumn.HeaderStyle>
<Style>
<Setter Property="FontSize" Value="14" />
<Setter Property="Background" Value="Pink"/>
</Style>
</sdk:DataGridTemplateColumn.HeaderStyle>
It's written that member FontSize and Background are not accessible.
What should i do?
Try changing it to:
<sdk:DataGridTemplateColumn.HeaderStyle>
<Style xmlns:primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"
TargetType="primitives:DataGridColumnHeader">
<Setter Property="FontSize" Value="14" />
<Setter Property="Background" Value="Pink"/>
</Style>
</sdk:DataGridTemplateColumn.HeaderStyle>
It should allow you set the properties with that namespace and target type added. You could also just add the namespace to the user control, instead of putting it in the style.
I'm currently developing a C# WPF application and I want the image to change to another when I hover over it. Below is my code and work fine. I'm just curious that why if I put my image format as .png, the application will stop working although it had no error. How can I handle this problem in order to make the application work if I use .png image since I found that the image shown is quite pixelated when I used .bmp?
<Image x:Name="CloseBtn" Width="19" Height="19" Margin="457,5,11,343" MouseLeftButtonDown="CloseBtn_MouseLeftButtonDown">
<Image.Style>
<Style TargetType="Image">
<Setter Property="Source" Value="/Resources/closeIcon.bmp"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Source" Value="/Resources/closeIconHover.bmp"/>
</Trigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>