Define a string as a static resource

You can define it as an application resource: <Application x:Class=”xxxxxx” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” xmlns:clr=”clr-namespace:System;assembly=mscorlib” StartupUri=”MainWindow.xaml”> <Application.Resources> <clr:String x:Key=”MyConstString”>My string</clr:String> </Application.Resources> </Application>

Set the focus on a TextBox in WPF XAML

You can use the FocusManager.FocusedElement attached property for this purpose. Here’s a piece of code that set the focus to TxtB by default. <StackPanel Orientation=”Vertical” FocusManager.FocusedElement=”{Binding ElementName=TxtB}”> <TextBox x:Name=”TxtA” Text=”A” /> <TextBox x:Name=”TxtB” Text=”B” /> </StackPanel> You can also use TxtB.Focus() in your code-behind if you don’t want to do this in XAML.

How do you do relative positioning in WPF?

Instead of putting (as in your example) a button directly on the canvas, you could put a stackpanel on the canvas, horizontally aligned, and put the two buttons in there. Like so: <Canvas> <StackPanel Canvas.Left=”100″ Canvas.Top=”100″ Orientation=”Horizontal”> <Button>Button 1</Button><Button>Button 2</Button> </StackPanel> </Canvas> I think that it’s quite flexible when you use more than 1 layout … Read more

How to get MessageBox.Show() to pop up in the middle of my WPF application?

Here’s a version of the MessageBoxEx helper class posted in the other thread that uses WPF style message boxes (Note that you still need to reference System.Drawing): using System; using System.Text; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; using System.Drawing; public class MessageBoxEx { private static IntPtr _owner; private static HookProc _hookProc; private static IntPtr _hHook; … Read more

Is there a way to comment out XAML that contains comments?

No, there is no way of having nested comments in XAML. You could use the mc:Ignorable attribute on your root element, and any attribute or element prefixed with that value will be ignored E.g: <UserControl … mc:Ignorable=”i”> <!– Ignore Text attribute –> <TextBlock i:Text=”Hello” /> <!– Ignore entire button –> <i:Button> </i:Button> </UserControl> Note that … Read more

WPF Label Foreground Color

I checked your XAML, it works fine – e.g. both labels have a gray foreground. My guess is that you have some style which is affecting the way it looks… Try moving your XAML to a brand-new window and see for yourself… Then, check if you have any themes or styles (in the Window.Resources for … Read more

WPF Datatrigger not firing when expected

Alternatively, you could replace your XAML with this: <TextBlock Margin=”0,0,5,0″ Text=”{Binding ElementName=EditListBox, Path=SelectedItems.Count}”/> <TextBlock> <TextBlock.Style> <Style TargetType=”{x:Type TextBlock}”> <Setter Property=”Text” Value=”items selected”/> <Style.Triggers> <DataTrigger Binding=”{Binding ElementName=EditListBox, Path=SelectedItems.Count}” Value=”1″> <Setter Property=”Text” Value=”item selected”/> </DataTrigger> </Style.Triggers> </Style> </TextBlock.Style> </TextBlock> Converters can solve a lot of binding problems but having a lot of specialized converters gets very messy.

WPF Trigger when property value is greater than a certain amount

You can use a data trigger and set the binding RelativeSource to Self. Data Triggers allow binding and bindings lets you have converters. Example: <Button Content=”I change colour depending on my width for some reason”> <Button.Triggers> <DataTrigger Binding=”{Binding Path=Width, RelativeSource={RelativeSource Self}, Converter={StaticResource isLessThanConverter}, ConverterParameter=50}” Value=”True”> <Setter Property=”Button.Background” Value=”Red” /> <DataTrigger> <Button.Triggers> <Button> Reference

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)