WPF custom style TextBox "Text" two-way binding failed [duplicate] - c#

Ok... this is leaving me scratching my head. I have two WPF controls--one's a user control and the other's a custom control. Let's call them UserFoo and CustomFoo. In the control template for CustomFoo, I use an instance of UserFoo which is a named part so I can get to it after the template is applied. That works fine.
Now both UserFoo and CustomFoo have a Text property defined on them (independently, i.e. not a shared DP using AddOwner. Don't ask...) that are both declared like this...
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text",
typeof(string),
typeof(UserFoo), // The other is CustomFoo
new FrameworkPropertyMetadata(
null,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
null,
null,
true,
UpdateSourceTrigger.PropertyChanged
)
);
Notice specifically that the mode is set to TwoWay and the UpdateSourceTrigger is set to PropertyChanged, again for both.
So in the style template for CustomFoo, I want to bind CustomFoo's Text property as the source to the internal UserFoo's Text property. Normally, this is easy. You just set UserFoo's text property to "{TemplateBinding Text}" but for some reason it's only going one way (i.e. UserFoo is properly set from CustomFoo, but not the reverse), even though again, both DPs are set for two-way! However, when using a relative source binding instead of a template binding, it works great! Um... wha??
// This one works
Text="{Binding Text, RelativeSource={RelativeSource AncestorType={local:CustomFoo}}, Mode=TwoWay}"
// As does this too...
Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
// But not this one!
Text="{TemplateBinding Text}"
So what gives? What am I missing?

Found this forum post on MSDN: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0bb3858c-30d6-4c3d-93bd-35ad0bb36bb4/
It says this:
A TemplateBinding is an optimized form of a Binding for template scenarios, analogous to a Binding constructed with
{Binding RelativeSource={RelativeSource TemplatedParent}}
Note from OP: Contrary to what it says in the documentation, in actuality, it should be this...
{Binding RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}
I filed a complaint against the docs, and while they did add a sentence now stating they are always one-way, the code example still doesn't list the mode, but I guess it's better than nothing.)
The TemplateBinding transfers data from the templated parent to the property that is template bound. If you need to transfer data in the opposite direction or both ways, create a Binding with RelativeSource of TemplatedParent with the Mode property set to OneWayToSource or TwoWay.
More in: http://msdn.microsoft.com/en-us/library/ms742882.aspx
Looks like Mode=OneWay is one of the "Optimizations" of using a TemplateBinding

TemplateBinding does not support two-way binding, only Binding does that. Even with your BindsTwoWayBeDefault option, it won't support two-way binding.
More info can be found here, but to summarize:
However, a TemplateBinding can only
transfer data in one direction: from
the templated parent to the element
with the TemplateBinding. If you need
to transfer data in the opposite
direction or both ways, a Binding with
RelativeSource of TemplatedParent is
your only option. For example,
interaction with a TextBox or Slider
within a template will only change a
property on the templated parent if
you use a two-way Binding.

Related

Force Refreshing WPF Bindings (one source)

I have this situation:
A IsToolbarButtonsEnabledProperty DependencyProperties
A have plenty of other DependencyProperties in a class (a huge class, needs to be this way)
A serie of Buttons on a toolbar.
The (IsEnabled) property of each of these buttons is a function of (IsToolbarButtonsEnabledProperty) throught a special converter, the buttons a differenced by ConvertParameter ("PreviousButton", "NextButton"...)
Opacity="{Binding IsEnabled, RelativeSource={RelativeSource Self}, Converter={StaticResource OpacityBoolToIntConverter}, UpdateSourceTrigger=PropertyChanged}"
IsEnabled="{Binding Path=DPEnableLinks, Converter={StaticResource ToolButtonEnableConverter}, ConverterParameter='ZoomOut' }"
ToolButtonEnableConverter is a converter that compares ConverterParameter "PreviousButton" with other value of other dependency property (in class). I have to many DP to make one multivalueconverter, so I read them straight from my class ((MainWindow)App.Current.MainWindow;)
Questions
When I update other DPs the value isEnabled / Opacity, dont change. How to fix this?
Is there a general solution to make a Binding refresh everytime a DP changes.
(Repeating myself): I will be adding more and more DPs over time, so a MultiValueConverter seams odd.
One way to force the Binding to update is to create a (meaningless) property and add it to the Binding (using MultiBinding), and when you want to update your Binding you change that property, and all the Binding is updated.
I must add that the more "straightforward" way is to use MultiBinding to all the relevant properties. If you have way to many properties that you need to bind, maybe you should re-think if you can build this functionality some other way.

How to get a Data Source generated details form to default to TwoWay Binding

I've got some classes in VS 2010 that I'm creating details forms for using the Data Sources window. Some types, like Guid, are bound to custom user controls (which have been set up with DefaultBindingProperty, etc). When I generate the form though, it sets regular control's default binding to TwoWay, where as my custom user controls are not. I get this in the XAML:
SelectedItemGUID="{Binding Path=client.ReportingContactGUID}"
whereas other controls get:
Text="{Binding Path=client.State, Mode=TwoWay, ValidatesOnExceptions=true,
NotifyOnValidationError=true}"
I don't care about the Validation error stuff, but I would like it if Mode=TwoWay appeared in there automatically. Is this an attribute or something I need to set?
I guess you want your custom Dependency Properties to bind twoWay be default which you can by passing FrameworkPropertyMetadataOptions.BindsTwoWayByDefault flag for FrameworkPropertyMetadataOptions while registering your custom dependency properties.
Sample:
public static readonly DependencyProperty SelectedItemGUIDProperty =
DependencyProperty.Register("SelectedItemGUID", typeof(Guid?),
typeof(YourUserControl), new FrameworkPropertyMetadata(null,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
This way you don't have to explicitly need to set Mode=TwoWay for bindings to this custom DP.

How can I bind a xaml property to a static variable in another class?

I have this xaml file in which I try to bind a Text-block Background to a static variable in another class, how can I achieve this ?
I know this might be silly but I just moved from Win-forms and feeling a little bit lost.
here is what I mean:
<TextBlock Text="some text"
TextWrapping="WrapWithOverflow"
Background="{Binding Path=SomeVariable}" />
First of all you can't bind to variable. You can bind only to properties from XAML.
For binding to static property you can do in this way (say you want to bind Text property of TextBlock) -
<TextBlock Text="{Binding Source={x:Static local:YourClassName.PropertyName}}"/>
where local is namespace where your class resides which you need to declare above in xaml file like this -
xmlns:local="clr-namespace:YourNameSpace"
You can't actually bind to a static property (INotifyPropertyChanged makes sense on instances only), so this should be enough...
{x:Static my:MyTestStaticClass.MyProperty}
or e.g.
<TextBox Text="{x:Static my:MyTestStaticClass.MyProperty}" Width="500" Height="100" />
make sure you include the namespace - i.e. define the my in the XAML like xmlns:my="clr-namespace:MyNamespace"
EDIT: binding from code
(There're some mixed answers on this part so I thought it made sense to expand, have it in one place)
OneTime binding:
You could just use textBlock.Text = MyStaticClass.Left (just careful where you place that, post-init)
TwoWay (or OneWayToSource) binding:
Binding binding = new Binding();
//binding.Source = typeof(MyStaticClass);
// System.InvalidOperationException: 'Binding.StaticSource cannot be set while using Binding.Source.'
binding.Path = new PropertyPath(typeof(MyStaticClass).GetProperty(nameof(MyStaticClass.Left)));
binding.Mode = BindingMode.TwoWay;
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
this.SetBinding(Window.LeftProperty, binding);
...of course if you're setting Binding from the code remove any bindings in XAML.
OneWay (property changes from the source):
And if you'd need to update the target (i.e. the control's property, Window.Left in this case) on the source property changes, that can't be achieved with the static class (as per my comment above, you'd need the INotifyPropertyChanged implemented, so you could just use a wrapper class, implement INotifyPropertyChanged and wire that to a static property of your interest (providing you know how to track you static property's changes, i.e. this is more of a 'design' issue from this point on, I'd suggest redesigning and putting it all within one 'non-static' class).
You can use the newer x:Bind to do this simply using:
<TextBlock Text="{x:Bind YourClassName.PropertyName}"/>

Passing values to DataTemplates with attached properties

I'm utilizing a data grid which has three columns of type DataGridTemplateColumn. These columns share almost identical behaviors and, as a consequence, utilize almost identical templates. The templates are copy-paste with a few resources changed out.
I would like to refactor the templates in to a generic version which uses an attached property to provide the necessary data. I've tried this but have been unable to access the property from inside the CellTemplate.
Methods I've tried are:
Bindings using RelativeSource: TemplatedParent.
Bindings using RelativeSource with various AncestorTypes.
Adding FrameworkPropertyMetadataOptions.Inherits to the FrameworkPropertyMetadata of the attached property.
The CellTemplate seems to have an odd degree of separation from its surroundings. What am I missing. If nothing, what is the appropriate solution to this problem?
A code example from you would have been helpful, but assuming that you have attached your property to the DataGrid, you should be able to bind to it from the CellTemplate using the following (untested):
<TextBlock Text="{Binding Path=(YourNamespace:YourClassName.YourAttachedPropertyName),
RelativeSource={RelativeSource AncestorType=DataGrid}, FallbackValue=''}" />
If you attached the property to the DataGridTemplateColumn, you would use:
<TextBlock Text="{Binding Path=(YourNamespace:YourClassName.YourAttachedPropertyName),
RelativeSource={RelativeSource AncestorType=DataGridTemplateColumn}, FallbackValue=''}" />
The FallbackValue property is not required, but it's a good practice to use it to avoid binding errors if the binding source can't be found.

Binding in DynamicResource ResourceKey wpf mvvm

I want to bind the style of button on the basis of if else condition. I have created one string property in the viewmodel and bind to the button's style attribute like this:
<Button x:Name="copd" Content="COPD"
Command="{Binding COPDReadingsCommand}"
Style="{DynamicResource ResourceKey={Binding CheckCopd}}"
HorizontalAlignment="Center" VerticalAlignment="Center"
Margin="20" FontWeight="Bold" />
I am looping through the resourceDictionary and getting all the keys. Using if else i am changing the string property value(CheckCopd) in if else.
I am getting the desired values in if else but style is not getting applied to the button when I execute my application. It only displays the generic button style.
How to bind the DynamicResource ?
Kindly Suggest?
Thank You.
You cannot use bindings on the DynamicResource properties, as it does not derive from DependencyObject. You would either need to set the Style property directly from code-behind, or just use a Binding.
You could use a Style for the Button type, which has a DataTrigger based on a custom property that dynamically changes the look. But in this case, you need a single Style, which changes it's setters based on your condition. It would not allow you to change the Style property itself dynamically.
You can try this... I came up with a way to create a DynamicResourceBinding on which you can use a converter to achieve the results you want. (You theoretically could also just use styles and triggers, but I digress...)
How do you create a DynamicResourceBinding that supports Converters, StringFormat?

Categories

Resources