I have several textboxes that are filled when adding a new record. If I cancel the adding before saving the record, only the textboxes are filled with content. I want to empty these boxes when canceling the action. I have a button to cancel the action. This button is bound to the Viewmodel on the Click action. I considered to use the code behind for this clearing action but I see no way of reaching the code behind from the viewmodel in My MVVM approach
<TextBox
Header="Coach"
PlaceholderText="naam coach"
Margin="8,8,16,8"
MinWidth="200"
x:Name="NaamTextBox"
Text="{x:Bind Viewmodel.NewCoach.Naam, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox
Header="Telefoon"
PlaceholderText="telefoon"
Margin="8,8,16,8"
MinWidth="200"
x:Name="TelefoonTextBox"
Text="{x:Bind Viewmodel.NewCoach.Telefoon, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<AppBarButton x:Name="DeleteNewRecord" Click="{x:Bind Viewmodel.DeleteNewRecord}" Icon="Cancel"/>
<AppBarButton x:Name="SaveNewRecord" Click="{x:Bind Viewmodel.SaveNewRecord}" Icon="Save"/>
I considered to use the code behind for this clearing action but I see no way of reaching the code behind from the viewmodel in my MVVM approach. Is there a simple way to clear data on a form before it's used by the viewmodel?
Related
I'm using a material design button in WPF and I'm not quite sure how to control the property IsIndeterminate from within C# code as the below does not work, like it usually does with "standard" properties like Content etc.
BTN_Search.materialDesign:ButtonProgressAssist.IsIndeterminate = true;
The WPF for the button below:
<Grid Width="124" Margin="5" VerticalAlignment="Center">
<Button Style="{StaticResource MaterialDesignRaisedButton}"
materialDesign:ButtonProgressAssist.Value="-1"
materialDesign:ButtonProgressAssist.IsIndicatorVisible="True"
materialDesign:ButtonProgressAssist.IsIndeterminate="False"
Content="Search"
Margin="2,0"
IsEnabled="{Binding DataContext.ControlsEnabled, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"
x:Name="BTN_Search"
Click="Search_Click"/>
</Grid>
I want the materialDesign:ButtonProgressAssist.IsIndeterminate property to be changed in the Click event when the button is pressed and then once I am done processing I need to change it back to False.
It is a dependency property so it can be set like this:
ButtonProgressAssist.SetIsIndeterminate(BTN_Search, true);
There is other examples on Github in the MaterialDesignInXamlTooking project https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/blob/master/MaterialDesignThemes.Wpf.Tests/ButtonProgressAssistTests.cs
I'm using C# in a WPF application with MVVM (with Caliburn Micro framework). I'm trying to bind 2 elements (one TextBlock and one TextBox) to the same property, that resides in my model view. My property is called FirstName.
I have two options to do the binding: Binding Path=FirstName or x:Name=FirstName. When I edit the textbox, I see the changes in the textblock only if I bind in a certain way (see code). Any idea of why the other way does not work? (when I type in the textbox I don't see my textblock updates)
I've tried different mode options (two ways, one way, etc). The NotifyOfPropertyChange seems to be working.
<!-- This works -->
<TextBlock Text="{Binding Path=FirstName}"/>
<TextBox x:Name="FirstName"/>
<!-- This does not work -->
<TextBlock x:Name="FirstName"/>
<TextBox Text="{Binding Path=FirstName, Mode=TwoWay}"/>
With your second example, you need to specify UpdateSourceTrigger=PropertyChanged:
<TextBlock x:Name="FirstName"/>
<TextBox Text="{Binding Path=FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
Otherwise, the source is only updated when the TextBox loses focus.
I am using this code to make a button in XAML within a MVVM project in UWP.
for this use this code:
<CommandBar Background="Yellow" >
<AppBarButton Label="Pruebas" Command="{Binding cmd_Process}" >
<AppBarButton.Icon>
<SymbolIcon Symbol="Setting"/>
</AppBarButton.Icon>
</AppBarButton>
</CommandBar>
At the moment of "clicking" the 'Command' the method that I need is executed in the ViewModel, but I have to send a content of a TextBlock to work with it in the ViewModel.
All good, but at the time of sending it sends me null, so that I do not send null I have to change the configuration of the Focus to the TextBlock, but I have to do it first, and then execute the Command.
If I use a normal button to link the Command works perfect, but not with the AppBarButton.
How do I make the AppBarButton change the focus of the TextBlock before executing the Command? Or how do I control the fucus from the ViewModel, knowing that I have to call the page from the ViewModel class?
you can set the focus through ViewModel or even from your page class before executing the command or within the command function. with Control.Focus() method. more info in the links below :
https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.control.focus
https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.input.focusmanager
https://learn.microsoft.com/en-us/windows/communitytoolkit/developer-tools/focustracker
You Can Lost the Focus in the TextBox with te property:
UpdateSourceTrigger=PropertyChanged
use into the TextBox... Example:
<TextBox x:Name="txt_Name" Header="Name:" Margin="5" Text="{Binding PropertyBindingToViewModel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
I have 3 conrtols placed on my Window in WPF, each control contains one or more TextBox and Button, when a new control is selected I would like to change the IsDefault to that button. What I am currently doing is when the TextBox's GotFocus event is fired, I change my Button to IsDefault. This works the first time but when I change from one control to another, and back to the first selected control the second controls IsDefault is still true and when enter is pressed it fires the Click event on the incorrect control.
Is there a way that i can clear all IsDefault properties on my window or what is a better solution to my current way of doing this? See image below for example, when enter is pressed the Button with "..." is fired instead of quick search.
XAML (Update)
<odc:OdcTextBox Text="{Binding AccountNumber, UpdateSourceTrigger=PropertyChanged}" MinWidth="100" Name="txtAccountNumber" Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" Margin="{Binding TextMargin}">
<odc:OdcTextBox.Buttons>
<Button IsDefault="{Binding ElementName=txtAccountNumber, Path=IsKeyboardFocusWithin}" Width="30" Style="{StaticResource DionysusButton}" x:Name="btnCdv" Content="...">
</Button>
</odc:OdcTextBox.Buttons>
</odc:OdcTextBox>
<Button IsDefault={Binding IsKeyboardFocusWithin, ElementName=ThisGroupBox} />
EDIT:
Your code is slightly different in that you are providing buttons to some custom control. These buttons are declared in a different scope to where they end up (ie I assume they end up inside the OdcTextBox template somewhere). I suspect this is why WPF can't find the named element since that element is named in an outer scope.
You could try using RelativeSource to find the parent OdcTextBox instead:
<Button IsDefault="{Binding IsKeyboardFocusWithin, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type odc:OdcTextBox}}}"
Width="30" Style="{StaticResource DionysusButton}" x:Name="btnCdv" Content="...">
Similarly to how you're using the TextBox GotFocus event, you could do the inverse of this and use the LostFocus event to reset the IsDefault Property
I am building an app for Windows Phone, and in this app I have a list of Movies with Title, Plot and Picture.
I have this list bound to a ListBox with a custom DataTemplate for the items showing the data. I also created a second page to show the details of each movie.
My problem now is the navigation between these pages. I'm using MVVM to build the applications and most of the approaches I've found searching on internet is to use the OnSelectionChanged event in the code-behind, but it goes agains what I want, that is to use MVVM.
Other approach I've seen, which is the one I'm trying, is to bind the SelectedItem to a property in the ViewModel, but I can't make it change the property, it seems that I cannot select an item in the listbox. Also, I don't have the visual feedback when I press one of the items in my listbox, like the feedback we have in the settings menu of the phone for example.
The code I'm using in the listbox is:
<ListBox Margin="0,0,-12,0" ItemsSource="{Binding Movies}" SelectedItem="{Binding SelectedMovieItem}" SelectionMode="Single" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,17">
<!--Replace rectangle with image-->
<Rectangle Height="50" Width="50" Fill="#FFE5001b" Margin="12,0,9,0"/>
<StackPanel Width="311">
<TextBlock Text="{Binding Name}" TextWrapping="NoWrap" Style="{StaticResource PhoneTextExtraLargeStyle}" Foreground="#000" />
<!--<TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>-->
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Another approach I've seen is to use the INavigationService to achieve this, I found this article: http://windowsphonegeek.com/articles/MVVM-in-real-life-Windows-Phone-applications-Part1
I read the parts one and two, but I couldn't understand this one works.
So, what I want to know is whether the approach I'm using is the correct to make a page navigation, or if there is a better way using MVVM to do this with visual feedback on the listbox.
Why is handling Event in the code behind against MVVM? Handling events interaction is part of the UI. Of course you won't all you code logic there. But you are trying just to go to the next page. I do something like this :
private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// If selected index is -1 (no selection) do nothing
if (MainListBox.SelectedIndex == -1)
return;
// Navigate to the new page
NavigationService.Navigate(new Uri("/Views/detailsPage.xaml?selectedItem=" + MainListBox.SelectedIndex, UriKind.Relative));
// Reset selected index to -1 (no selection)
MainListBox.SelectedIndex = -1;
}