Bind a textblock to static property which isn't in the VM - wpf

This is my xaml code:
<TextBlock Text="{Binding MyTranslations[0].Name}"></TextBlock>
What I want to do is remove the 0. Instead of 0, I need to get the correct integer from a static field in a static class which is in another project but in the samo solution.
I guess it should look like something like this:
<TextBlock Text="{Binding MyTranslations[MyStaticClass.MyStaticInt].Name}"></TextBlock>
How do I do this?

There is probably some strange way to do this syntactically in xaml, but usually when I come across strange problems like this, I usually just make a calculated property in my VM.
public string MyCurrentTranslation
{
get { return MyTranslations[MyStaticClass.MyStaticInt].Name; }
}
Then just bind to that property:
<TextBlock Text="{Binding MyCurrentTranslation}"/>

Related

Binding to dictionary using enum as key

I came across a problem with binding to dictonary property using enum as key in xaml file.
I have a ViewModel with property
public Dictionary<EColor, MyDataStatus> StatusByColor {get; set;}
Where EColor is as enum and MyDataStatus is my own class with property Value.
In a view I'm trying to create a binding
<TextBlock Text="{Binding StatusByColor[enum:EColor.eRed].Value}"/>
and I get an binding error
BindingExpression path error: '[]' property not found on 'object' ''Dictionary`2 .....
However if I enter a digit (instead of using enum) like this:
<TextBlock Text="{Binding StatusByColor[0].Value}"/>
It works perfectly fine
I thought it may be a problem with "including" enum in xaml (it is defined in other assembly) but a few lines below i can use it as CommandParameter like this.
Text="{Binding Path=StatusByColor, Mode=OneWay,
Converter={StaticResource statusByColorToDayElapsedConverter},
ConverterParameter={x:Static enum:EPaintColor.eRed}}"
And in this scenario (with converterParameter) even IntelliSense suggests enum's values.
This is how I "include" enum in xaml:
"xmlns:enum="clr-namespace:CommonTypes.Enums;assembly=CommonTypes""
Firstly,Give my answer,and I have verified it.
<TextBlock Text="{Binding StatusByColor[eRed].Value}"/>
I tried your code with a sample and get the same result as you mentioned.
But when I try this code.
<TextBlock Text="{Binding StatusByColor[0].Value}"/>
I changed the index from 0 to 999.I got an exception
"System.Collections.Generic.KeyNotFoundException"
So I confirm that there is a type casting from Int to Enum.
However,only Int can be converted? How about string?
Yes!String also worked.
Refer:
WPF Binding to items within a dictionary by key?

How can I add value to existing textbox in xaml

I have the following TextBlock:
<TextBlock VerticalAlignment="Vertical" Text="Name" />
This is the TextBlock for my button on which I want to display name and value.
I want to display text on the button as Name[some_value].
I'm getting this this some_value run-time from some function. How can I print the value here?
It seems like you're not yet familiar with Binding in XAML.
You can read, and see examples on MSDN here.
Go to ViewModel of that View:
private string _buttonValue;
public string ButtonValue{
get {return _buttonValue;}
}
Go to constructor and write down:
_buttonValue = "Name " + put_here_SomeValue_Runtime_Value;
Now, to to .xaml
Bind like belw:
<TextBlock Text="{Binding ButtonValue, Mode=OneWay}"/>

Why to use Path in silverlight databinding?

I am new to the silverlight. I found some articles on the internet for databinding. I can see that binding is performed in different ways as follows
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Path=Name}"/>
<TextBlock Text="{Binding Path=Account}"/>
<TextBlock Text="{Binding Path=Property1.Property2.Property3}"/>
In the first textblock binding is performed with property name. In second example also binding is performed with property name. Then what is different with Path in second textblock ? I know how to do binding for the first three textblock but I am not aware how to do binding with the fourth textblock ? and when we use the binding of the following type
<TextBlock Text="{Binding Path=Property1.Property2.Property3}"/>
Can you please give me coding example along with explanation for all above thing ? If I misunderstood something then please guide me.
By using the following binding:
<TextBlock Text="{Binding Path=Property1.Property2.Property3}"/>
You're saying that the data context of the TextBlock has a property called Property1, which returns an object that has a property called Property2, which in turn has a property called Property3. The value returned by Property3 is what will be shown in the TextBox.
For example, if your classes looked like this and the data context for the TextBox was an instance of Foo, you'd see "Hello World" displayed:
public class Foo
{
public Foo1 Property1 { get; set; }
}
public class Foo1
{
public Foo2 Property2 { get; set; }
}
public class Foo2
{
public string Property3 { get { return "Hello World"; } }
}
The "." syntax just lets you refer to "subproperties" of an object. For more information, take a look at this MSDN article.
There is no difference between the first and the second TextBlock in your question.
Path is the 'default' property of the Binding object that gets set.

Binding property to Silverlight dependency property independent of DataContext

I'm trying to make an Address control that has an IsReadOnly property, which will make every TextBox inside read only when set to true.
<my:AddressControl Grid.Column="1" Margin="5" IsReadOnly="True"/>
I've managed to do this just fine with a dependency property and it works.
Here's a simple class with the dependency property declared :
public partial class AddressControl : UserControl
{
public AddressControl()
{
InitializeComponent();
this.DataContext = this;
}
public static readonly DependencyProperty IsReadOnlyProperty =
DependencyProperty.Register("IsReadOnly", typeof(bool),
typeof(AddressControl), null);
public bool IsReadOnly
{
get { return (bool)GetValue(IsReadOnlyProperty); }
set { SetValue(IsReadOnlyProperty, value); }
}
}
In the XAML for this codebehind file I have a Textbox for each address line:
<TextBox IsReadOnly="{Binding IsReadOnly}" Text="{Binding City, Mode=TwoWay}"/>
<TextBox IsReadOnly="{Binding IsReadOnly}" Text="{Binding State, Mode=TwoWay}"/>
<TextBox IsReadOnly="{Binding IsReadOnly}" Text="{Binding Zip, Mode=TwoWay}"/>
Like i said this works just fine.
The problem is that the Address control itself is bound to its parent object (I have several addresses I am binding).
<my:AddressControl DataContext="{Binding ShippingAddress, Mode=TwoWay}" IsReadOnly="True">
<my:AddressControl DataContext="{Binding BillingAddress, Mode=TwoWay}" IsReadOnly="True">
The problem is that as soon as I set DataContext to something other than 'this' then the binding for IsReadOnly breaks. Not surprising because its looking for IsReadOnly on the Address data entity and it doesn't exist or belong there.
I've tried just about every combination of binding attributes to get IsReadOnly to bind to the AddressControl obejct but can't get it working.
I've tried things like this, but I can't get IsReadOnly to bind independently to the AddressControl property instead of its DataContext.
<TextBox IsReadOnly="{Binding RelativeSource={RelativeSource Self}, Path=IsReadOnlyProperty}" Text="{Binding City, Mode=TwoWay}" />
I think I'm pretty close. What am I doing wrong?
With this answer (actually my own answer to a similar question) I have a good [better] solution.
I still have to iterate through the textboxes, but I don't have to set the actual value. I can create bindings in the codebehind - just not with XAML.
I think you're stuck, at least, if you want to do this just via binding. My guess is that you're going to have to resort to code-behind, presumably by iterating through your child textbox controls and setting their IsReadOnly propert as a side-effect of your Address control's IsReadOnly property.
Unlike some folks who think that any code sitting in a code-behind file is effectively an admission of failure, I don't get religious about it: if throwing some code into a code-behind is the easiest way to do something, that's where I put my code. On the contrary, if I have to spend half a day trying to figure out how to do something via binding that I could do in five minutes with a code-behind, that's failure, IMO.

WPF : An alternative way to specify a ValueConverter on binding

Most common way I encountered of specifying a value converter for a binding is to:
1. Create an instance of the value converter as a resource with a key.
2. Reference the instance using StaticResource markup extension:
<TextBlock Text="{Binding Converter={StaticResource myFormatter}" />
Q: Is there anything wrong with using static instance as follows:
<TextBlock Text="{Binding Path=Description, Converter={x:Static local:MyFormatter.Instance}}"/>
// where Instance is declared as:
public readonly static MyFormatter Instance = new MyFormatter();
In my case value converter is immutable.
Edit: another way is to turn the converter into an extension
so that you specify the converter using markup extension format:
<TextBlock Text="{Binding Converter={local:MyFormatter}}"/>
Technically it will be fine, but in practice I don't like it:
If you declare the converter as a resource, then you have a single point of reference. If you change the namespace or class name of the converter, then you only have a single place to update.
If you declare it as static, then you need to bring the clr-namespace in at the top of each and every xaml file that uses the converter. If you declare it as a resource, you don't.
{Binding Converter={StaticResource myFormatter} is much shorter and easier to read than the static one. In the long run, this will help you more than you might think.
As long as the formatter really has no state, this should be fine. It is not equivalent though. In the first case, you have an instance of the class for each instance of your XAML-based control. In the second, only one instance will ever be created.

Resources