My sample code is below.
I want to pass 'Go to linked item' to ConverterParameter but I can't because the string has spaces.
Text="{Binding Value,
Source={x:Static local:Dictionary.Instance},
Converter={StaticResource StringConverter},
ConverterParameter=Go to linked item, Mode=OneWay}"
How can I do this?
Option 1
Text="{Binding Value,
Source={x:Static local:Dictionary.Instance},
Converter={StaticResource StringConverter},
ConverterParameter='Go to linked item', Mode=OneWay}"
Option 2
If you want to use this in multiple places add a string resource.
<sys:String x:Key="GoToLink">Go to linked item</sys:String>
And pass the resource key.
ConverterParameter={StaticResource ResourceKey=GoToLink}}
If your string has spaces then wrap it in single quotes, double quotes won't work; this is probably due to the fact that the entire text field is wrapped in double quotes and so using them again within the binding would incorrectly indicate closure.
Text="{Binding Value,
Source={x:Static local:Dictionary.Instance},
Converter={StaticResource StringConverter},
ConverterParameter='Go to linked item', Mode=OneWay}"
Related
From here https://msdn.microsoft.com/en-us/library/system.windows.data.bindingbase.targetnullvalue(v=vs.110).aspx,
It gives an example of specifying 'TargetNullValue':
<TextBox Width="150"
Text="{Binding Source={StaticResource object2},
Path=PropertyB, BindingGroupName=bindingGroup,
TargetNullValue=please enter a string}" />
My question is how can I specify an empty string for TargetNullValue?
I have tried "TargetNullValue= }" (there is a space between = and }, but that does not work, the target null value is null, instead of an empty string.
Thank you.
You can use x:Static in the xaml and define the string.empty there.
<TextBox Width="150"
Text="{Binding Source={StaticResource object2},
Path=PropertyB, BindingGroupName=bindingGroup,
TargetNullValue={x:Static system:String.Empty} }" />
You'll need to add the appropriate namespace to the xaml as needed. VS should do this for you though. Namespace required is
xmlns:system="clr-namespace:System;assembly=mscorlib"
I don't know why the answer saying to use '' is deleted, but it works fine for me:
<TextBox Text="{Binding NumberOfCats, TargetNullValue=''}" />
I have just succeeded to use, on c#, the property Binding, I saw two ways to do this:
Binding="{Binding Path=DataBinded}" and Binding="{Binding Path=.DataBinded}"
They are both working, but if there is two ways to write, it's for a reason...What's the difference between Path= and Path=. ?
usually . refers to the preceding object and allow you to point to the sub properties, you may consider it as a separator as well. as mentioned in the question there is no preceding object so in this case the . refers to the DataContext itself and so Binding="{Binding Path=DataBinded}" and Binding="{Binding Path=.DataBinded}" are equal
you may consider the following example when you want to bind some text value directly
<TextBlock Text="{Binding}" />
or
<TextBlock Text="{Binding Path=.}" />
both of the example above points to the DataContext of the TextBlock and will bind to the same.
Using a period path ("Path=.") will bind to the current source :)
You can read the docs here under remarks seccion (last point)
This may well have been asked and answered before but I really wasn't sure how to phrase the question.
I have a dictionary (MyLookup) and I want a control to bind to a particular key and value in the dictionary. The key for the dictionary is a string, and lets say the particular item is 'MyItem'. The output should be something like:
MyItem value: 43
One idea was to use a couple of text blocks and a tack panel, one for the key and one for the value. I tried to encapsulate the item of interest in the stack panel, but things get fruity when defining the binding path for the second text block
<StackPanel Orientation="Horizontal" Tag="MyItem">
<TextBlock Text ="{Binding Path=Tag, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}}, StringFormat={0} value:}"/>
<TextBlock Text ="{Binding Path=MyLookup[{Binding Path=Tag, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}}]}"/>
</StackPanel>
It might also be possible to do it with a single text block and some string formatting?
<TextBlock Text="{Binding Path=MyLookup[MyItem], StringFormat={0} value: {1}}"/>
Bleugh, it all falls apart there as there need to be 2 outputs?! My current thinking is to use a multi-converter to take the string and convert it to a string and an int, but I was wondering if thee were cunning things that I'm missing.
Once again, A Binding can only be set on a DependencyProperty of a DependencyObject. You're trying to use a Binding for the integer that specifies which key/value to look at from your Dictionary, but you can't because it is an integer and not a DependencyProperty.
Instead of doing this, you should create a view model that contains all of the data and functionality that your view requires. As #HighCore mentioned, once you have organised your data in the right way, then all of these nasty problems will disappear.
Hmm, what the h**k is this {Binding Path=MyLookup[{Binding Path=Tag, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}}]}
Take a look at ObjectDataProvider.
That bad ass allows you do craziest Bindings you can think of and all that is happening in XAML.
http://msdn.microsoft.com/en-us/library/system.windows.data.objectdataprovider%28v=vs.110%29.aspx
I currently have a list of objects in which my RadGridView's ItemsSource is set to. When the property "DoNotContact" of the object in the list has been set to True, I want to hide the information in the cell that contains a Phone number within my RadGridView. As you can see in my XAML, I'm setting the Visibility property within the TextBlock like so:
<telerik:GridViewDataColumn Header="Evening" DataMemberBinding="{Binding Path=EveningPhone}" Width="75" SortMemberPath="EveningPhone">
<telerik:GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Visibility="{Binding Path=DoNotContact, Converter={StaticResource BoolToVisibilityConverter}}">
<Hyperlink Click="MakeEveningCallHandler">
<TextBlock Text="{Binding Path=EveningPhone}" />
</Hyperlink>
</TextBlock>
</DataTemplate>
</telerik:GridViewColumn.CellTemplate>
</telerik:GridViewDataColumn>
When attempting to debug it, the Converter is never hit and although I can see the property "DoNotContact" has been set, the phone number still shows. The converter itself works fine as I've used it in other occasions. Again I only want to hide the information WITHIN the cell for the "Evening" Property, not the actual column itself. Any Ideas what's going wrong here? Thanks a bunch!
The code you provided works for me!
Does anyone know if its possible to do a binding and (if not how to achieve the same effect) on the same property using more than one binding in sort of a template
i.e.
A textblock that has Text bound in the expression
"{Binding Path=Contact.Title} {Binding Path=Contact.Firstname} {Binding Path=Contact.Surname}"
all in one text property
Not a big deal just:
<TextBlock>
<Run Text="{Binding Path=Contact.Title}"/>
<Run Text="{Binding Path=Contact.Firstname}"/>
<Run Text="{Binding Path=Contact.Surname}"/>
</TextBlock>
AFAIK it's not possible.
This is one of the reasons to follow the MVVM pattern, create an intermediary view which reflects the data in a format that you actually want presented, so you would create a fullname property on that class that was a concatenation of those fields and then bind to that.
Value Converters are one solution for binding to multiple values:
http://timheuer.com/blog/archive/2008/07/30/format-data-in-silverlight-databinding-valueconverter.aspx#11262
In that scenario you'd bind your TextBlock's Text property to the Contact object and specify a custom value converter that you've created. The converter can perform the string formatting based on property values.
I don't think it is possible to do it directly in xaml. I would absolutely love multiple bindings to one property.
What I have learned however, is that you can accomplish things similar to this using a couple different strategies:
Using a Stackpanel:
<StackPanel Orientation="Horizontal">
<TextBlock Text="Hello, "/>
<TextBlock Text="{Binding Contact.Title}"/>
<TextBlock Text="{Binding Contact.Firstname}"/>
<TextBlock Text="{Binding Contact.Surname}"/>
<TextBlock Text="!"/>
</StackPanel>
Using a Converter:
<TextBlock Text="{Binding Contact,
Converter={StaticResource ContactNameConverter}}"/>
More Info On Converters