using a Resource Variable in a Textblock - c#

So I have a project where I am setting up a resource variable named TEST (Right clicking on the project in the Solution Explorer, choosing Properties, then choosing Resources) and I want to pass those into a textbox on a WPF XAML form. It seems to be different than if I try to use a normal variable but alas, I am befuddled.
Can anyone guide me please on how to pass the variable thru please?
Thank you.
I know this is not the correct method
======================================
<TextBlock Text="{Binding Bottle_1_Title}" FontWeight="Bold" FontSize="32" HorizontalAlignment="Center" TextAlignment="Center"></TextBlock>
This is also not valid and I am not seeing the intellisense to help me
<TextBlock Text="{resource.test}"

Related

"The member "X" is not recognized or is not accessible." error in WPF

I'm trying to make an interface with multiple language options on a WPF screen. I'm writing the name of the text block I want to add to my Resources file and my code, but on the xaml screen "The member "Manuals" is not recognized or is not accessible." I get an error. I am getting this error in all the buttons and texts I try to add. Can you help me understand why? There is my xaml code line and resources designer cs project code line. Thanks in advance.
public static string _Manual
{
get
{
return ResourceManager.GetString("Manuals", resourceCulture);
}
}
<TextBlock Name="ttbManuals" Grid.Row="8" Text="{x:Static resx:Resources._Manual}" HorizontalAlignment="Left" Margin="0,0,0,-20" VerticalAlignment="Top" Height="39" Width="580" Style="{DynamicResource CncTextBlock}" Background="{DynamicResource BackgroundBrush1}" Padding="10,5,0,0"/>
The Resources class is by default created as internal. For use in XAML it needs however to be public.
To fix it, select the resx file in solution explorer and in the properties, change ResXFileCodeGenerator to PublicResXFileCodeGenerator.
See also here: Visual Studio - Resx File default 'internal' to 'public'

Textbox has no ScrollBar Property

Visual Studio's IntelliSense keeps blowing up at me when I try to set a vertical scrollbar on my textbox with C#. It says that 'TextBox' has no definition for Scrollbars and suggests that I'm missing something.
I think I must be missing something because everything I find online just assumes it works by typing txtMyTextBox.Scrollbar = true; but it doesn't work for me. I don't really know what I'm doing wrong. I haven't added any using statements, just the default that get loaded when using WPF, but no tutorials ever point to me needing to add one.
<TextBox
Height="200"
AcceptsReturn="True"
Text="{Binding ... }"
TextWrapping="Wrap"
VerticalScrollBarVisibility="Visible" />

Get a MenuItem to switch to a different visible window when clicked

I am working with C# in MS Visual Studio Community 2013. I am trying to set up a MenuItem so that when clicked it causes the current window to hide itself and another window appears at the same location.
I have a couple of books on the subject and I've also spent lots of time searching for answers online. However, I simply can't find out how to set this up when it's something that should be relatively straightforward to do.
I've done it before with older versions of C#, but it just seems impossible to sort out with Visual Studio Community 2013.
At this stage I have a menu and menu items set up something like the 'much simplified' following:
<Menu HorizontalAlignment="Left" Height="21" Margin="10,10,0,0" VerticalAlignment="Top" Width="1264" Grid.ColumnSpan="2" FontWeight="Medium" FontSize="14">
<MenuItem Header="Item Type" Margin="0" Width="100" Height="21" FontSize="11" Click="MenuItem_Click">
<MenuItem Header="Item Action" HorizontalAlignment="Left" Width="185"/>
</MenuItem>
</Menu>
Most, if not all of that was generated automatically, as I set up the menu in Visual Studio.
Also, if I right click on the 'Item Action' menu item, a popup appears with a 'view code' option. If I click that option, the following empty function appears in the corresponding *.cs file:
private void MenuItem_Click(object sender, RoutedEventArgs e) { }
I realise that I probably need to add code to that empty method to do what I need to do, but so far I haven't found out how to do that.
There is also a 'Command' property in the properties list for the MenuItem. I also assume that that may play a part in sorting this out, but at this stage I don't know how.
Please let me know how this needs to be set up and big thanks in advance for any help.
Also, after searching elsewhere on stackoverflow, I found the basics for hiding/showing different windows. I've got that working OK, but it's not enough to resolve my problem.
Basically, in my real menu I have multiple menuItems and the clicking of each one needs to be dealt with in a different way. Therefore, in the MenuItem_Click function I need a way of identifying which MenuItem was actually clicked, so that I can respond in the correct way for each and every MenuItem click.
You can add click event for each menu item and add code for each method.inside this method you can add this code
Window1.Visibility = Visibility.Hidden;
window2.Visibility = Visibility.Visible;
you can use Command property when you need to bind methods in MVVM Pattern

Type reference cannot find public type named

I am getting error like "Type reference cannot find public type named 'Sign'" in xaml. how can i resolve it. the Sign class is in the same assembly.
<DataTemplate DataType="{x:Type local:Sign}">
<Expander Padding="4"
IsExpanded="{Binding RelativeSource={
RelativeSource Mode=FindAncestor, AncestorType={
x:Type ListBoxItem}}, Path=IsSelected}">
<Expander.Header>
<TextBlock Text="{Binding Name}" ... />
</Expander.Header>
<DockPanel LastChildFill="True">
<Border DockPanel.Dock="Left" CornerRadius="16" BorderBrush="WhiteSmoke" Background="AliceBlue" BorderThickness="5" HorizontalAlignment="Center" VerticalAlignment="Center">
<Image Source="{Binding Icon}" Width="90" Height="90" Stretch="Fill" />
</Border>
...
</DockPanel>
</Expander>
If the type resides in the same assembly as the XAML you are modifying, omit the assembly segment of the clr-namespace when you import the namespace.
DO
xmlns:local="clr-namespace:NamespaceContainingSignClass"
DO NOT
xmlns:local="clr-namespace:NamespaceContainingSignClass;assembly=AssemblyContainingSignClassAndXAML"
For those in my boat who weren't helped by the top 1,00 results for this error on Google....in my case it was the precise opposite of the advice from smelch: i had to ADD the assembly info on the end of my xmlns declaration. Likely because of my particular circumstances, i guess - my datatemplate was in a stand-alone resourcedictionary xaml file. Not sure. I just know it didn't work until i added the assembly info, so for those floundering out there give that a whack and see if it works. i'm not inclined to dive into why, it just did.
Check if the root tag of the xaml file has the namespace with class Sign mapped to local alias: xmlns:local="clr-namespace:NamespaceContainingSignClass"
Check whether class Sign is marked public
Check whether class Sign has parameterless constructor
Go through and check all of the answers to this question - I'm sure one of them is right, but I don't know which one worked for me.
Save your project, close Visual Studio, re-open Visual Studio.
Voila, it now magically works, despite not changing anything.
Swear at Microsoft under your breath, and carry on with your life...
Ran into this error right now. The problem was that the class I was referencing from my assembly was in an assembly that had a higher framework level(4.7.2 against 4.6). Version alignment solved the problem.
I just discovered another issue, that may cause this problem.
It is allowed to use dots in project names, e.g.
FancyTrainsimTools.Desktop
is a valid project name. If you use folders in the project, you may get constructs like this:
FancyTrainsimTools.Desktop.Logic
If you define data templates referencing items in this folder, this results in error messages. I also used a hierarchical datatemplate, but this gives slightly different messages.
The workaround:
Rename your project by removing the dot:
FancyTrainsimToolsDesktop
Edit: I try to reproduce the issue again, in order to report a bug, but now it all works.... So mystery not yet solved and not clear what to do to create w ork around....

Expose property in Windows Phone 7 custom ControlTemplate

I'm using Visual Studio 2010 & Expression Blend 4, the target is Windows Phone 7 platform.
What I would like to make is a custom control (specifically a custom PushPin for Map, but could be anything) and expose some of it's properties so I could change them. I'll try to explain better with an example:
<ControlTemplate x:Key="PushpinControlTemplate1" TargetType="Microsoft_Phone_Controls_Maps:Pushpin">
<Border BorderBrush="#FF0012AD" BorderThickness="3" Background="#FF0012AD" Width="32" Height="32" CornerRadius="5">
<TextBlock Text="2" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="32" Margin="0,-4,0,0"/>
</Border>
</ControlTemplate>
This example is from MSDN example.
Now, when I "use" this template on a map, it shows as expected, but with one problem. I would like that the Text property of the TextBlock (in this case... for example it could be ImageSource if I put an image into the PushPin) could be changed in the properties panel of Expression Blend, and in the C# code "behind" the XAML - of course, for each "instance" of this PushPin separately.
As far as I know, it has to do something with Dependency properties (I could be wrong?), but I am yet to find a clear example showing exactly WHAT, WHERE (C# / XAML) and WHY (sorry, I had to emphasize) had to be done.
I grasped most of the Phone 7 "topics" but now I'm a little stuck regarding resources and data bindings, as shown here :)
If someone could provide some sample code or a link to a good tutorial I would be grateful. Thanks!
If you want to simply set a user control property by code, you can use a normal property. If however you want to bind to the property, you must use a dependency property.
Good example here

Categories

Resources