In analogy to this question, I am curious how to interpret the expression {Binding ''} in WPF.
Note that there are two apostrophes after Binding.
In addition to a Google search I looked at this link provided in the linked question above, but was not able to assert the meaning of the two apostrophes after Binding.
What I came across were String Format expressions, where the apostrophe is used inside double quotes to denote another String expression. But I doubt this is the case here, for this question.
What does this expression mean?
It's equivalent to an empty Path. The apostrophes simply enclose whatever you write between them. So in your case it's a Binding to the DataSource (without a Path) - although I have to say, I've never seen it used that way.
The reason you probably didn't find this in any Bindings related context is because it's actually a feature that is available to all XAML markup extensions (like Binding, Static, StaticResource, etc.).
MSDN: Details about how markup extensions are parsed
The text value of either a MEMBERNAME or STRING is read as follows. Leading whitespace characters are consumed without being represented in the generated token. If the first non-whitespace character is a quote (either Unicode code point 0022, Quotation Mark, or 0027, Apostrophe), the tokenizer proceeds as follows:
The first quote is consumed and is not represented in the token’s value.
The text value becomes the characters up to but not including the next matching quote (i.e. a character of the same code point as the opening quote) that is not preceded by a “\” character. All these characters and also the closing quote are consumed. Any “\” characters in the resulting text value are removed.
Whitespace characters following the closing quote are consumed, and are not represented in the token.
Take this simple (and rather useless) extension for example:
public class StringExtension : MarkupExtension
{
public StringExtension()
{ }
public StringExtension(string value)
{
Value = value;
}
public string Value { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
return Value;
}
}
The extension could be used like this (all identical results):
<!-- via constructor (1 argument) -->
<TextBlock Text="{local:String text}"/>
<!-- via constructor (1 argument) -->
<TextBlock Text="{local:String 'text'}"/>
<!-- via empty constructor + named property -->
<TextBlock Text="{local:String Value=text}"/>
<!-- via empty constructor + named property -->
<TextBlock Text="{local:String Value='text'}"/>
So, what are the 's used for? Well for example for leading and trailing Whitespaces.
<!-- no whitespaces -->
<TextBlock Text="{local:String text }"/>
<!-- whitespaces -->
<TextBlock Text="{local:String ' text '}"/>
Related
Image im=new Image();
string ii="123456789";
im.Name=ii;
It's ok
but
Image im=new Image();
string ii="123.456.789";
im.Name=ii;
It throw exception. Why is it denied to allocate comma "." to control Name property?
The code you wrote is probably not what you want. As MDoobie said, there are restrictions on what the value of Name can be. The Image class is inheriting the Name property from its immediate parent class, "System.Windows.FrameworkElement." Follow MDoobie's "msdn" link to see what the purpose of that Name attribute is. Near the end of the Remarks you will see a link that will lead you to the specific information about Name restrictions.
In WPF, Names have some restrictions (for instance it cannot contains dots).
"The string values used for Name have some restrictions, as imposed by the underlying x:Name Directive defined by the XAML specification. Most notably, a Name must start with a letter or the underscore character (_), and must contain only letters, digits, or underscores. " (from msdn)
In my WPF project, usability tests show that users can type either dot or comma as a decimal separator at numeric input textboxes.
In order to cleanup that, I created a converter that replaces commas with dots, or dots with commas, and it works, but only if the culture uses the separator that is being replaced.
This is the code that is inside my converter's ConvertBack method:
return System.Convert.ToDouble(((string)value).Replace(',', '.');
My eyes hurt when I look at this, because it is an evident hack, and it is causing lots of errors, for sometimes it's necessary to replace comma, and sometimes to replace dot. We are in the verge of implementing actual localization in our software, so I ask:
"What would be the proper way to do it, that is, allow the user to use either comma or dot, without spoiling all the neat localization infrastructure?"
After some research following the very wise suggestion from Stewbob, I decided to only allow the user to input the current culture decimal separator. For that, I listen to PreviewTextInput in code behind.
The effect is that the user can only type numbers, then the current decimal separator once, then more numbers. Other characters simply "don't respond". We think this is fair usability-wise.
private void PreviewNumberInput(object sender,
System.Windows.Input.TextCompositionEventArgs e) {
string input = ((TextBox)sender).Text + e.Text;
string pattern = "^[0-9]+[" +
Thread.CurrentThread.CurrentUICulture.NumberFormat.NumberDecimalSeparator +
"]?([0-9]+)?$";
Regex regex = new Regex(pattern);
e.Handled = !regex.IsMatch(input);
}
The value of the second parameter of the markup extension I am using is a string containing some commas and I don't want those commas to be interpreted as parameter separators by the xaml interpreter / parser, but that the whole string as such including the commas is used as value.
Here is an example:
<SomeControl SomeProperty="{Wpf:MyExtension MyFirstParameter,
MySecondParameter, being a string, containing some commas.}" />
Google didn't help, I found some similar issues but none apply to this problem:
http://msdn.microsoft.com/de-de/library/ms744986.aspx
http://msdn.microsoft.com/en-us/library/ms748250.aspx
You can use single quotes to encapsulate a string; so your mark-up should look something like:
<SomeControl SomeProperty="{Wpf:MyExtension MyFirstParameter,
'MySecondParameter, being a string, containing some commas.'}" />
I'm not sure whether you will also need the {} escape sequence mark-up.
I have a String "0214" i would like te present this as 02:14 is this possible in wpf with stringFormat and binding.
If it would be an integer i could do it like bellow but the problem is that the "0" disapears and becomes Int "214" But this is not good for placing in the database because it requires 4 numbers.
<GridViewColumn Header="Start" DisplayMemberBinding="{Binding Start, StringFormat={}{0:00\:00}}"/>
output: '02:14'
StringFormat is not actually used to format strings. It's used to format non-string values into strings.
You can use an IValueConverter however to convert your string into the format you want. Another option would be to alter your data class to expose a read-only FormattedStart property which returns the formatted value.
Use a converter to split the input string and return the two parts separated by a :
Convert to an int and use StringFormat={}{0:00\\:00}} (This should be the equivalent of int.ToString("00':'00")
I am attempting to set the Name property of a Page in the constructor:
public partial class PageListView : Page
{
public PageListView(string title)
{
InitializeComponent();
Name = title;
}
}
However, I often get the following error message.
'x' is not a valid value for property 'Name'.
Where x seems to be almost anything, drilling down into the exception details doesn't seem to provide any useful information (e.g. the InnerException is null.)
Does anyone know what is happening here?
The Name property generally follows the rules of C#/VB.NET identifiers (i.e. fields). Based on the documentation:
The string values used for Name have some restrictions, as imposed by
the underlying x:Name Directive defined by the XAML specification.
Most notably, a Name must start with a letter or the underscore character
(_), and must contain only letters, digits, or underscores.
Based on the parameter you are passing (i.e. title), it seems like you may violate that. But you'd have to give some specific examples to be sure.
Of course, moments after posting this I realised what's going on.
Because FrameworkElement.Name is used for creating object references, you have to ensure that the string contains only valid chars for an object instance variable name.
Use Title or another plain text property instead, unless you really want to set the x:Name property for referencing.