Trigger Properties in XAML - c#

I am beginning to learn XAML and I am following along with the MCTS Self-Paced Training book. I've come to the section about triggers (more specifically Property Triggers) and I stumbled upon something that is rather annoying.
Here is my code:
<Style TargetType="Button">
<Style.Triggers>
<Trigger Property=""
</Style.Triggers>
</Style>
Now, when I want to get the property that Trigger points to I want Intellisense to list all the available properties and it doesn't. Is that something I'm doing, a problem with VS, or is it just not supported in that scenario. Besides this, it works when I set the Setter for the Trigger. For example:
<Setter Property="INTELLISENSE WORKS" Value="Something" />
I'd really like for the properties of the Button to show up like IsMouseOver.

Check your version of visual studio and upgrade to Visual Studio 2010 Service Pack 1 that everything should work as expected (Y).
Visual Studio 2010 (without the service pack) does not have intellisense for triggers.

Related

WPF - Import custom fonts in C#

I would like to import custom fonts on my WPF application so that they work without having the client to install them.
All the answers I have found so far are in XAML, I would like to do it only in C#.
My fonts are in Resources/Fonts/.
I have already tried this :
Fonts.GetFontFamilies(new Uri("pack://application:,,,/Resources/Fonts/#"));
But it didn't work.
I did everything bluetoothfx said but it still did not work.
Then I changed the Build action of my fonts (it was to Content), to Embedded Resource, and it worked. Resource works also for me.
Thanks anyway.
I think the way that you are working will not work.
At first create a folder name fonts then Add the font to your project, change its Build Action to Content.
Now you need to find the internal name (Real name) of the font not the font-file name. You can have it by opening the font file and you can see it on top.
Now edit App.xaml
<Application.resources>
<style x:key="MYFONT_INTERNAL_NAME">
<setter property="TextElement.FontFamily"
value="pack://application:,,,/fonts/#MYFONT_INTERNAL_NAME" />
</style>
Now use it in your code like:
<TextBlock Style="{StaticResource MYFONT_INTERNAL_NAME}" FontSize="16" Text="Font Style" />
To know more search here:
http://www.alteridem.net/2014/02/24/custom-fonts-in-wpf-applications/

StaticResource is null even though it's defined in XAML?

I've got a bit of a niche scenario which I'm fully not expecting anyone to solve, but thought I'd give it a shot:
I have a Windows Forms User Control, which contains a single ElementHostcontrol that has it's Child property set to a WPF / XAML user control.
I'm getting some strange behaviour whereby the XAML contains something that looks like this:
<UserControl>
<UserControl.Resources>
<somenamespace:myresource x:Key="foo" />
</UserControl.Resources>
<Grid DataContext="{StaticResource foo}">
...
</Grid>
</UserControl>
I get a XamlParseException at runtime, asking me to Provide a value, even though it's clearly defined above.
I can see InitializeComponent() is being called, but that's as far as I can step.
In my code behind, in the WPF / XAML user control, if before InitializeComponent() is called I add:
this.Resources["foo"] = new myresource() everything works great again.
Just to confuse matters even further, this only happens when I use Visual Studio 2015 to compile the code - compiling using VS2013 (on the same machine/no solution changes whatsoever) works perfectly.
Has anyone got any ideas, or even helps on how to debug this?

Invalid value for 'Event'-Property (XAML Eventsetter)

I'm using Visual Studio 2015 Community and I get the following error message:
Invalid value for 'Event'-Property:
Microsoft.VisualStudio.DesignTools.Xaml.LanguageService.Semantics.XmlValue.
Here's the code behind:
<Style x:Key="TextBoxStyle1" BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
<EventSetter Event="MouseEnter" Handler="Check_MouseEnter" />
<EventSetter Event="MouseLeave" Handler="Check_MouseLeave" />
<EventSetter Event="GotFocus" Handler="Check_GotFocus" />
I've tried UIElement.MouseEnter, Mouse.MouseEnter, TextBox.MouseEnter. If I compile the handler works just fine, but the error message is still there.
Any suggestions?
This seems a bug in the WPF designer, as already reported here on Microsoft Connect.
It seems that the designer falsely gives an warning or error, but eventually the code is okay, so it compiles and works. Nothing you should worry about now, since the product isn't released yet.
Finally, 7.5 years after this question, the bug is fixed since VS 2022 version 17.5 Preview 2.
On the Release channel, the fix is expected to be available from February 2023.

The name ... does not exist in the namespace

This problem is pretty much the same as this one. However my problem is that I get this error but it does compile run and find it find.
My XAML with the error is a resource dictionary (in a different project) and it is trying to use an enum called MainViewMode in a style. I have inherited the code from a previous team so I do not know why it was originally written like this but the xaml consists of all the path data for icons (using the <geometry> tag) as well as all of the Styles. I don't think this is relevant to the problem but just thought I would highlight it anyway.
As I said, when the program runs it does work fine however the designer mode is not working on one of the forms I need to modify and it is making it really annoying.
xmlns:cenum="clr-namespace:ABC;assembly=DEF"
...
<Style x:Key="MainViewToggleButtonStyle" TargetType="ToggleButton">
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="CommandParameter" Value="{x:Static cenum:MainViewMode.Overview}" />
...
</style>
I have a resource dictionary, it calls an enum in a different project and says that it can't find it. It does however find it at runtime and work fine.
The main issue is that the designer doesn't load for any forms using this resource dictionary.
I believe your problem is that your namespace is incorrect. Take a look at the top answer in the question you linked to and notice how they have the example namespace. Since you're getting the enum from a different project you will also want to add that project as a reference.
To get the namespace right find the folder that the enum class is in, or if it's not in a folder just the project name. The namespace will be like one of the following,
If it's in a folder try something like this
ProjectName.FolderName;assembly=ProjectName
If not try something like this
ProjectName;assembly=ProjectName

Build Custom Theme or Use Standard Theme in WPF

Soon to be a professional .NET developer (I hope) I start to dig into Windows Presentation Foundation (WPF). Looking into several video tutorials, I find design of GUI a daunting task. Having to specify every color, on every element, in every situation, to every platform seems a bit too much. How can you make this process simpler, and more generic when it comes to design? Is there any templates to start from, or is one expected to specify a couple of hundred rows of XAML before the design is looking appealing?
Considering the code-block below...
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="LightGreen" />
<Setter Property="Foreground" Value="DarkGreen" />
</Style>
... where properties for hover and pushed-button style is left out which need additional rows of XAML to do what the developer wants.
Might there be a simple XAML-editor around to increase productivity? If there isn't, its just to dig dip into XAML and start building styles too keep for later projects.
Designing your own theme is great but it requires a lot of expertise and time. From my point of view its not a great idea to invest in designing your own theme, provided you already have so many themes availabe online. You can take one which suits you and modify it as per your needs.
I genrally refer these links for themes -
WPF Themes -
http://wpfthemes.codeplex.com/
http://wpf.codeplex.com/wikipage?title=WPF%20Themes&ProjectName=wpf
WPF Theme Selector
http://wpfthemeselector.codeplex.com/
Wpf Project With 21 Theme
https://marketplace.visualstudio.com/items?itemName=AliAlikhani.SahaWpfTheme2012
In case you need more options you can buy one here -
http://www.xamltemplates.net/wpf-themes/
There is no requirement to create a Style. You can just use the default style. Consider this simple messagebox style window:
<Window x:Class="MyProject.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Test" Height="217" Width="298">
<StackPanel Orientation="Vertical">
<Label>Here is the Message.</Label>
<StackPanel Orientation="Horizontal">
<Button>OK</Button>
<Button>Cancel</Button>
</StackPanel>
</StackPanel>
</Window>
If you really need to re-style controls, I would pick and choose which ones to do. Otherwise, yes I think creating a custom style is a pretty big task.
Creating a custom style is a very large task, however, should you decide this is neccesary (it's not required). You can use Expression Blend to speed up the process.
Reuxables have a couple of free themes you can try. We've just bought one of their non-free ones and it's dead easy, you just throw in a reference in your app.xaml and it transforms your app. Easy to tailor, too.

Categories