I want to use a Converter to change the value of a StaticResource before assigning it to a property. Is there a way to simulate a Binding that will just set the value of the StaticResource after converting it?
Something like {Binding Value={StaticResource myStatic}, Converter={StaticResource myConverter}}?
This works:
<TextBox Text="{Binding Source={StaticResource myStatic},
Converter={StaticResource myConverter},
Mode=OneWay}" />
Note that you have to bind one way, because the binding requires a path attribute otherwise. This makes sense, as otherwise the binding would have to replace the whole resource...
Related
ValueFormattingConverter.Convert is called with the XmlElement. ConvertBack is never called. Why? Is there some obligation to pass binding directives down the chain? Is the use of the TextBox overriding its own binding settings? What can be done?
My TextBox
<TextBox Width="200"
Text="{Binding Path=., Converter={StaticResource valueFormattingConverter}}",
Mode=TwoWay,
NotifyOnSourceUpdated=True,
NotifyOnValidationError=True,
UpdateSourceTrigger=PropertyChanged}" />
Usage is rather convoluted. Starting at the top, we provide an XML element to a tab.
<TabItem.DataContext>
<Binding Source="{StaticResource mcf}",
XPath="mdf/press_information"/>
</TabItem.DataContext>
That tab contains a ItemsControl which builds TextBoxes through this ControlChooser which passes the binding along.
<ItemsControl.ItemTemplate>
<DataTemplate>
<W3V:ControlChooser RelativeSource="{RelativeSource AncestorType=W3V:ObjectList}",
Content="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
My converter class header. Convert method is called. ConvertBack never.
[ValueConversion(typeof(XmlElement), typeof(string))]
public class ValueFormattingConverter : IValueConverter
EDIT: The chosen answer basically says Path=. doesn't support 2-way binding. I believe it is the correct answer to the question. Very helpful to know, but "can't do that" doesn't solve the larger problem. So I have laid out the larger question here: Means of generating an editable form from XML.
The binding direction to source won't work with a {Binding Path=.}. This is because there is no bound property, but just the binding source object.
Hence there will never be a source update, and the ConvertBack method is never called, because that would mean to replace the source object.
In order to make your code work, you would have to bind to some property:
<TextBox Text="{Binding Path=SomeElement, ...}"/>
Anyone got a situation to bind the same DataContext to Text property (for example) in TextBlock.
I have to assign the DataContext to reflect some trigger based on the Data values from Datacontext in my style. at the same time, i need to bind with the same DataContext object to get the Text Property After applying some conversion on either IValueConverter/IMultivalueConverter.
As i know {Binding}, just bind with the current datacontext. But in the same scenario how to use converter with it?
Any suggestions will be appreciated.
<TextBlock Style="{StaticResource DataEntryTextBlock1}" Grid.Row="1"
DataContext="{Binding MyField1}"
Text="{Binding MyField1, Converter={StaticResource myConverter}}">
</TextBlock>
This XAML script does not work, as the Text binding is trying to look for the MyField1 variable inside the MyField1.
Thanks,
Vinodh
{Binding} is equivalent to {Binding Path=.} so in you case you can use
Text="{Binding Path=., Converter={StaticResource myConverter}}"
Binding.Path on MSDN
Optionally, a period (.) path can be used to bind to the current source. For example, Text="{Binding}" is equivalent to Text="{Binding Path=.}"
I have a question that is connected with setting path when binding in XAML, using WPF.
Imagine that my DataContext is of PropertyInfo type. PropertyInfo contains data about Property Name.
And in that object I nest (for example) TextBox which Text property I would like to bind to property with that name of another's element DataContext.
Something like that [it's pseudocode because it's not possible that way]:
<DataTemplate>
<TextBox Text={Binding ElementName=someElement, Path=DataContext. + {Binding Path=Name}}/>
</DataTemplate>
I want to create flexible view that's why I need to solve that problem.
Is there a way to achieve this without code behind?
Considering all the above, I think that I can make my question short and simply ask whether there is a way to concatenate string while setting binding's path.
I'm not sure if I understand correctly but is this something that multibinding would assist with?
<TextBlock Grid.Row="3" Grid.Column="1" Padding="5"><TextBlock.Text>
<MultiBinding StringFormat="[{0}, {1}]">
<Binding Path="LastName"></Binding>
<Binding Path="FirstName"></Binding>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Can i bind to the name property?
This does not seem to work:
<TextBlock Name="FordPerfect" Text="{Binding Path=Name, Mode=OneWay}"/>
Am i doing something wrong?
Edit:
Adding ElementName=FordPerfect" solved the issue. What i don't understand is why only binding to Name required this while other properties don't.
Note: Moved the second (design) issue to another question (where i should have placed in the first time...)
Thanks
I would try this :
<TextBlock Name="FordPerfect"
Text="{Binding ElementName=FordPerfect, Path=Name, Converter={StaticResource conv}, Mode=OneWay}"/>
This way, your TextBlock will be the context of the binding.
If it does not work, watch the Output window, you should find a binding error !
you could have more easily done this:
<TextBlock Name="FordPerfect"
Text="{Binding Name, Converter={StaticResource conv}, Mode=OneWay, RelativeSource={RelativeSource Self}}"/>
As to why: that textbox' DataContext is not automatically the TextBox itself. So binding to Name tries to bind to whateverObjectInDataContext.Name. So either you set the DataContext beforehand like:
<TextBlock Name="FordPerfect" DataContext={Binding RelativeSource={RelativeSource Self}}
Text="{Binding Name, Converter={StaticResource conv}, Mode=OneWay}"/>
... or directly set a Source for the Binding
The issue you're having is a Binding, by default, uses the DataContext of the element it's used on as its source. However you want the binding source to be the TextBlock element itself.
WPF has a class called RelativeSource which, as its name implies, sets the source relative to the binding. One of the relations you can choose is Self which does exactly what you want: sets the source of the binding to the element it's used on.
Here's the code:
<TextBlock Name="FordPerfect" Text="{Binding Name, RelativeSource={RelativeSource Self}}" />
Since you're already setting the source with RelativeSource, you don't need to specify ElementName. You also don't need Mode=OneWay as a TextBlock.TextProperty already defaults to one-way since it's output-only.
Hope this helps!
I have a ComboBox that uses a DataTemplate. The DataTemplate contains a binding which uses an IValueConverter to convert an enumerated value into a string. The problem is that the value converter is never invoked. If I put a breakpoint in StatusToTextConverter.Convert(), it never is hit.
This is my XAML:
<ComboBox ItemsSource="{Binding Path=StatusChoices, Mode=OneWay}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource StatusToTextConverter}}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
I thought this is how one implicitly binds to the value a DataTemplate is presenting. Am I wrong?
Edit: For context: I intend to display an Image in the DataTemplate alongside that TextBox. If I can't get the TextBox binding to work, then I don't think the Image will work, either.
In some circumstances you must explicitly supply a Path for a Binding. Try this instead:
<TextBlock Text="{Binding Path=.,Converter={StaticResource StatusToTextConverter}}"/>