TextBox BindingExpression UpdateSourceTrigger=Explicit - wpf

Need a TextBox in a ListView DataTemplate to call set on either LostFocus or enter key. Used UpdateSourceTrigger = Explicit and events for LostFocus and KeyUp. Problem is that I cannot get a valid reference to the BindingExpression.
XAML
<ListView x:Name="lvMVitems" ItemsSource="{Binding Path=DF.DocFieldStringMVitemValues, Mode=OneWay}">
<ListView.View>
<GridView>
<GridViewColumn x:Name="gvcExistingValue">
<GridViewColumnHeader Content="Value"/>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="tbExistingValue"
Text="{Binding Path=FieldItemValue, Mode=TwoWay, ValidatesOnExceptions=True, ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=Explicit}"
Validation.Error="Validataion_Error"
LostFocus="tbExistingValue_LostFocus" KeyUp="tbExistingValue_KeyUp" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Code Behind NOT working
private void tbExistingValue_LostFocus(object sender, RoutedEventArgs e)
{
BindingExpression be = lvMVitems.GetBindingExpression(ListView.SelectedItemProperty);
be.UpdateSource();
}
be is null. I have tried ListView.SelectedValueProperty and ListView.SelectedPathProperty. If it try tbExistingValue it fails with a message "does not exists" and will not even compile. How do I get the proper BindingExpression?? Thanks.
If I set UpdateSourceTrigger = LostFocus and remove the event handlers it does call set properly. There is a valid twoway binding there. I just cannot get a valid reference to BindingExpression (be) using explicit.
It works fine for a TextBox directly on page (in a grid cell). The xaml below works:
<TextBox Grid.Row="1" Grid.Column="1" x:Name="strAddRow"
Text="{Binding Path=DF.NewFieldValue, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True, UpdateSourceTrigger=Explicit}"
Validation.Error="Validataion_Error"
LostFocus="strAddRow_LostFocus" KeyUp="strAddRow_KeyUp"/>
This BindingExpression works fine:
private void strAddRow_LostFocus(object sender, RoutedEventArgs e)
{
BindingExpression be = strAddRow.GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();
}

Since you apply the binding on your Textbox's Text DP, so you need to fetch the binding from there only like this -
private void tbExistingValue_LostFocus(object sender, RoutedEventArgs e)
{
BindingExpression be = (sender as TextBox).GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();
}
Moreover, you haven't bind the ListView SelectedItem with any property of your ViewModel. To retrieve the binding, it should be atleast binded to some value. So, you should bind it to your FieldValueProperty then you won't get null value with your code in place.

You do not need to use UpdateSourceTrigger on TextBox using the event LostFocus.
It's the functioning by default.
Answer here : https://msdn.microsoft.com/en-gb/library/system.windows.data.binding.updatesourcetrigger(v=vs.110).aspx

Related

How to get the checkbox column sender's datagrid parent

Hi I have a datagrid (xaml code below):
<DataGrid.Columns>
<DataGridTextColumn Header="FLEET" IsReadOnly="True" Width="1*" Binding="{Binding FLEET, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTemplateColumn Header="SELECTED?" Width="1*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Viewbox Margin="-0.2" Height="18.5">
<CheckBox IsThreeState="True" IsChecked="{Binding Path=isSelected, UpdateSourceTrigger=PropertyChanged}" Click="FSC_CheckBox_Click"/>
</Viewbox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
The click events:
private void FSC_CheckBox_Click(object sender, RoutedEventArgs e)
{
......
}
Is there a way I can get the the parent datagrid object from the click sender? I've tried this link
Get containing row from a CheckBox inside of a DataGrid, but it didn't work because of the viewbox and datatemplate(I guess).
Any suggestions is appreciated. Thank you.
The click event handler will get the sender, which you can cast to checkbox.
I reckon the simplest approach is to abuse the Tag property on your checkbox.
That can be any old object you fancy.
You can use a relativesource binding for that.
Something like:
<CheckBox Tag="{Binding RelativeSource={Relativesource AncestorType=DataGrid}}"
Grab it off there, roughly:
private void FSC_CheckBox_Click(object sender, RoutedEventArgs e)
{
var cb=(CheckBox)sender;
var parentDataGrid = (DataGrid)cb.Tag;
}
This is air code, I don't have a datagrid with a checkbox and a click handler to try it out with easily so there may be some typo lurking.

Wpf Combobox with databinding: initial value is empty

In the following xaml fragment SessoList is a list of string ("M" and "F").
<ComboBox IsEditable="False" Margin="5" SelectedValue="{Binding Sesso}" ItemsSource="{Binding SessoList}" Width="40" Height="28"/>
The combobox works as expected and it is pre-populated reflecting the value of Sesso in the viewmodel.
The combobox selectable items are only two and fixed so I tried to simplify defining them in xaml:
<ComboBox IsEditable="False" Margin="5" SelectedValue="{Binding Sesso}" SelectedValuePath="{Binding Tag}" Width="40" Height="28" Name="Primo">
<ComboBoxItem Content="M" Tag="M" />
<ComboBoxItem Content="F" Tag="F" />
</ComboBox>
This combobox is capable of updating the viewmodel property sesso, but is not pre-populated with the correct value.
The following error is reported:
BindingExpression path error: 'Tag' property not found on 'object'
How can I successfully define the combobox items in xaml and have it display the right value based on SelectedValue databinding ?
Forgot to mention I'm using .Net 4.0
As I can understand, you want to define the ComboBox ItemsSource in XAML,
here is the solution that worked for me:
Xaml Window resources:
<Window.Resources>
<x:Array x:Key="Array" Type="{x:Type nirHelpingOvalButton:ComboObjectModel}">
<nirHelpingOvalButton:ComboObjectModel Content="M_Content" Tag="M_Tag"/>
<nirHelpingOvalButton:ComboObjectModel Content="F_Content" Tag="F_Tag"/>
</x:Array>
Xaml Combo:
<Grid>
<ComboBox IsSynchronizedWithCurrentItem="True" IsEditable="False" SelectedIndex="0" Margin="5" ItemsSource="{StaticResource Array}"
SelectedValue="{Binding Content, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
SelectedValuePath="Tag" Width="90" Height="28" Name="Primo">
<i:Interaction.Behaviors>
<nirHelpingOvalButton:CustomComboSelectionBehavior/>
</i:Interaction.Behaviors>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Content}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox></Grid>
View model combo SelectedValue binded property:
public string Content
{
get { return _content; }
set
{
_content = value;
OnPropertyChanged("Content");
}
}
List item Behavior Code:
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
var firstItem = AssociatedObject.ItemsSource.Cast<object>().FirstOrDefault();
AssociatedObject.SelectedItem = firstItem;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.Loaded -= OnLoaded;
}
regards

ListBox with hyperlink -> selection changed

I want to do with xaml bindings such feature:
Listbox contains hyperlinks.
When hyperlink clicked - we go to another frame
But also SelectedItem must changed, and on another frame we show info about selected item.
I want it without subscribing click/selected events. Only declarative
Example of my listbox
<ListBox Grid.Row="1"
x:Name="OrderTypesListBox"
ItemsSource="{Binding OrderTypes, Mode=OneWay}"
SelectedItem="{Binding SelectedCall.OrderType, Mode=TwoWay}"
>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" />
<HyperlinkButton Style="{StaticResource LinkStyle}" NavigateUri="/WindowPage" TargetName="ContentFrame" Content="WindowPage"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Now solve like that
<ListBox Grid.Row="1"
x:Name="OrderTypesListBox"
ItemsSource="{Binding OrderTypes, Mode=OneWay}"
SelectedItem="{Binding SelectedCall.OrderType, Mode=TwoWay}"
>
<ListBox.ItemTemplate>
<DataTemplate>
<HyperlinkButton
TargetName="ContentFrame"
NavigateUri="{Binding OrderTypeNextPage}"
Content="{Binding Name}"
Click="HyperlinkButton_Click"
Tag="{Binding}"
/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
{
OrderTypesListBox.SelectedItem = (sender as HyperlinkButton).Tag;
}
Don't use a HyperlinkButton. Perform the needed actions when the SelectedItem changes in your ViewModel.
Edit: If you need to respond to all click events even if the item is already selected then you can use a Behavior to do this. Just create a behavior for the TextBlock that navigates on the TextBlock click event.
Edit2: Behaviors are pretty simple to code up and easy to use (and don't break the MVVM paradigm).
public class NavigatingTextBlockBehavior : Behavior<TextBlock>
{
protected override void OnAttached()
{
AssociatedObject.MouseLeftButtonDown += new MouseButtonEventHandler(OnMouseDown);
}
private void OnMouseDown(object sender, MouseButtonEventArgs e)
{
NavigationService.Navigate(new Uri("/WindowPage"));
}
}

WPF: weird problem in dataBinding with TabControl

I'm trying to use DataBinding for dynamically populating a TabControl but have a problem. dataBinding runs fine but I would like the content of each TabItem to be independent one from the other. Here is my XAML code:
<TabControl
DockPanel.Dock="Left"
ItemsSource="{Binding OpenChats}"
Name="tabChats"
VerticalAlignment="Top"
Width="571">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock
Text="{Binding Name}" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<TextBox />
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
TabItems are created with different headers (as I want) but when the user types something in the TextBox inside the ContentTemplate, the same text is maintained in different tabItems and I don't want this.
What am I doing wrong?
I had same problem. This answer helped me. My solution was to remove focus from textbox when tab changed. When focus from textbox is removed, new content is set to binded property as expected.
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DependencyObject focusedElement = (FocusManager.GetFocusedElement(tabControl) as DependencyObject);
if (focusedElement != null)
{
DependencyObject ancestor = VisualTreeHelper.GetParent(focusedElement);
while (ancestor != null)
{
var element = ancestor as UIElement;
if (element != null && element.Focusable)
{
element.Focus();
break;
}
ancestor = VisualTreeHelper.GetParent(ancestor);
}
}
}
or use
Text="{Binding UpdateSourceTrigger=PropertyChanged}"
on textbox binding.
The TextBox in the ContentTemplate has no Binding. Try
<TabControl.ContentTemplate>
<DataTemplate>
<TextBox Text="{Binding}" />
</DataTemplate>
</TabControl.ContentTemplate>
Adjust the bindingpath if necessary

WPF TextBox StringFormat not working with PropertyChanged

I have a problem. I need to have double formatted values in all TextBoxes.
When you type something into this, after lost focus it will be formatted.
<TextBox Text="{Binding ABC, StringFormat='{}{0:N}'}" />
Problem arises when you add this UpdateSourceTrigger with propertychanged. Then it will never be formatted.
<TextBox Text="{Binding ABC, UpdateSourceTrigger=PropertyChanged, StringFormat='{}{0:N}'}" />
Why is that? Is there any way how to solve that? (in XAML preferably)
Try this
<TextBox x:Name="test" Text="{Binding MyName, UpdateSourceTrigger=Explicit,StringFormat='{}{0:N}'}" TextChanged="test_TextChanged" Width="100" Height="30" />
private void test_TextChanged(object sender, TextChangedEventArgs e)
{
BindingExpression exp = test.GetBindingExpression(TextBox.TextProperty);
exp.UpdateSource();
}

Resources