Use resource file.resx for WPF Controls content attribute [duplicate] - c#

I'm newbe in WPF.
I need to add reference on the resources file resx and get strings from it.
<Window x:Class="SelectObjectsWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:res="clr-namespace:SelectObjectsWindow.Properties"
Title="{x:Static res:Resources.Res1}" Height="350" Width="525">
<Grid>
....
</Grid>
I got an error: The name "Resources" does not exist in the namespace "clr-namespace:SelectObjectsWindow.Properties".
I was searching information about this and find couple of advises:
Set "public" access modifier of resx file.
Change Build Action property of resx on "Embedded Resource".
So, I did it, but nothing changed.
I checked all namespaces. Seems to be ok.
I don't know, what else I should try.
In .cs i can do this without error
var res = Properties.Resources.Res1;
Do somebody know, what is wrong?

Change access modifier from internal to public, your binding will work.

Try adding an Assembly reference:
xmlns:res="clr-namespace:SelectObjectsWindow.Properties;assembly=[AssemblyName]"
Replace [AssemblyName] with the name of your assembly which can be found by right clicking your project and selecting Properties, go to the Application tab and see Assembly name:

Related

Extended WPF Toolkit - Change Wizard style

I am working on a WPF application that uses the Wizard window from the Extended WPF Toolkit. I need to change the color of the footer of the wizard and unfortunately the developers didn't expose any property to do it, so I need to edit the style.
The Toolkit is imported as NuGet package, so I cannot just edit the source code. I found the default style of the control (Generic.xaml) on Codeplex, copied it in a file in my project so now I have something like this:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Xceed.Wpf.Toolkit"
xmlns:conv="clr-namespace:Xceed.Wpf.Toolkit.Core.Converters">
<conv:WizardPageButtonVisibilityConverter x:Key="WizardPageButtonVisibilityConverter" />
<Style TargetType="{x:Type local:Wizard}">
...
Here I get two errors:
The type 'conv:WizardPageButtonVisibilityConverter' was not found.
Verify that you are not missing as assembly reference and that all
referenced assemblies have been built.
and
The name "Wizard" does not exist in the namespace
"clr-namespace:Xceed.Wpf.Toolkit".
Then I tried to change the line
xmlns:local="clr-namespace:Xceed.Wpf.Toolkit"
to
xmlns:local="http://schemas.xceed.com/wpf/xaml/toolkit"
and the second error disappeared, but I don't know how to deal with the first one.
Do you have any idea? Is it the right way to change the default style?
Thanks!
The XAML namespace mapping should also specify the name of the assembly in which the WizardPageButtonVisibilityConverter class is defined:
xmlns:conv="clr-namespace:Xceed.Wpf.Toolkit.Core.Converters;assembly=Xceed.Wpf.Toolkit"

Can't find "Resources" file in wpf

I'm newbe in WPF.
I need to add reference on the resources file resx and get strings from it.
<Window x:Class="SelectObjectsWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:res="clr-namespace:SelectObjectsWindow.Properties"
Title="{x:Static res:Resources.Res1}" Height="350" Width="525">
<Grid>
....
</Grid>
I got an error: The name "Resources" does not exist in the namespace "clr-namespace:SelectObjectsWindow.Properties".
I was searching information about this and find couple of advises:
Set "public" access modifier of resx file.
Change Build Action property of resx on "Embedded Resource".
So, I did it, but nothing changed.
I checked all namespaces. Seems to be ok.
I don't know, what else I should try.
In .cs i can do this without error
var res = Properties.Resources.Res1;
Do somebody know, what is wrong?
Change access modifier from internal to public, your binding will work.
Try adding an Assembly reference:
xmlns:res="clr-namespace:SelectObjectsWindow.Properties;assembly=[AssemblyName]"
Replace [AssemblyName] with the name of your assembly which can be found by right clicking your project and selecting Properties, go to the Application tab and see Assembly name:

DataContext is not bind in the XAML file

I am new to WPF + MVVM architecture. In my application, I am trying to implement "DataContext" in the XAML itself as below
<Window x:Class="MyWPF.UI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:MyWPF.UI.ViewModel"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<vm:Northwind_DataViewModel></vm:Northwind_DataViewModel>
</Window.DataContext>
<Grid>
</Grid>
</Window>
I am getting "The name 'Northwind_DataViewModel' does not exists in the namespace 'clr-namespace:MyWPF.UI.ViewModel".
I have ViewModel file. Before using "DataContext", I built this application.
Could you please let me know what is wrong with my code?
thanks
The name 'Northwind_DataViewModel' does not exists in the namespace 'clr-namespace:MyWPF.UI.ViewModel".
Chances are, you compiler is right. There is no class named MyWPF.UI.ViewModel.Northwind_DataViewModel in your project. Make sure it exists, has a public, parameterless constructor, is not an inner class of something and not generic. If that's the case, it will work.
In case namespace is declared in separate assembly from where your XAML resides, you have to provide assembly name as well.
Sample:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Similarly, provide your assembly name where this namespace resides (Assuming assembly name is MyWPF.UI):
xmlns:vm="clr-namespace:MyWPF.UI.ViewModel;assembly=MyWPF.UI"
Thanks a lot your responses. Finally I got the solution. Actually, this project was placed in a shared folder. So, I moved it to my local drive (C:). Now, it is working fine without any issues.
I guess, in shared path / shared folder, it may not work. Please let me know if I am wrong.
thanks again.
In order to solve this problem, you need to understand how the DataContext works. This answer contains a lot of hints and links to supporting tutorials:
ReSharper WPF error: "Cannot resolve symbol "MyVariable" due to unknown DataContext"

TextBox custom control header problems

I'm currently trying to use this TextBox AutoComplete custom control :
http://www.codeproject.com/Articles/26535/WPF-Autocomplete-Textbox-Control?msg=3484969#xx3484969xx
This is the first time I'm adding such external things in my project and when I add the header line in my Xaml file like explained in the article :
<UserControl x:Class="Maha.Gestion.Note_de_frais.SaisieNoteDeFrais"
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:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:local="clr-namespace:WPFAutoCompleteTextbox"
>
VisualStudio tell me that clr-namespace is not included in the assembly.
I'm sure this is something totally trivial but after few hours still can't find any solution.
All files asked by the author of the article has been added of course.
Someone can explain me the good way to add such external resources ?
Thanks
xmlns stands for XML NameSpace. It basically links a prefix of your choice to a namespace. In your case, it links the local prefix with the namespace declared in the file that you want to use.
You can find out more by looking at the XAML Namespaces and Namespace Mapping for WPF XAML article at MSDN.
Please check that the file that you want to use has this exact namespace declared.
Please remove all white space from your xmlns declarations - you may have extraneous characters hidden there.
Like this:
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:local="clr-namespace:WPFAutoCompleteTextbox">

Visual Studio refactor-rename causes inexplicable global replace of attributes in XAML file

I feel like I must be going crazy, but I just changed the name of a property in a view model (a C# file) from Width to Size using Visual Studio's refactor-rename feature. When I was done, this error appeared in the Error window:
Error 2: The property 'Size' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'.
The error was referring to a XAML UserControl file. When I checked the file to see what was up, I realized that every attribute named Width had been changed to Size.
Example:
<UserControl x:Class="ApbSymbolGenerator.Views.Symbol"
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"
Size="{Binding Size}"
Height="{Binding Size}">
This is the first time I've ever had refactor-rename cause a change to a XAML file (besides the x:class value). (Note: I did not do a global find/replace, I did refactor-rename. I undid everything and performed the rename again, and it did the same thing.)
Strangely, it only affected one of several XAML files in my app that has a Width property.
Any explanation what could be going on here?
Looks like this is a bug (that won't be fixed) - Refactor Rename Bug

Categories