Note that there are some explanatory texts on larger screens.

plurals
  1. POCan we have buttons in a validation template, and how can we bind it to the ViewModel method?
    text
    copied!<p>Requirement: <br> Need to display an error message when the user types a forlder name that doesn't exist as shown below: <img src="https://i.stack.imgur.com/Yemlq.jpg" alt="alt text"> <br></p> <p>Problem: I am able to display the UI but not able to call a method in the view model when the user clicks on the button "CreateNew"</p> <p>View Model Code:</p> <pre><code> public string this[string columnName] { get { return "The entered folder name doesn't exist."; } } RelayCommand createNewFolder; public RelayCommand CreateNewFolder { get { if (createNewFolder == null) createNewFolder = new RelayCommand(param =&gt; this.OnCreateNewFolder()); return createNewFolder; } } public void OnCreateNewFolder() { MessageBox.Show("CreateNewFolder"); } </code></pre> <p><br><br> RelayCommand.cs can be downloaded at: <a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=mag200902MVVM&amp;DownloadId=4357" rel="nofollow noreferrer">http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=mag200902MVVM&amp;DownloadId=4357</a></p> <p><br><br></p> <p>Xaml Code:</p> <p></p> <pre><code>&lt;Window.Resources&gt; &lt;ControlTemplate x:Key="validationTemplate"&gt; &lt;DockPanel LastChildFill="True"&gt; &lt;Border Margin="5,5,0,0" DockPanel.Dock="Bottom" Background="Red"&gt; &lt;StackPanel&gt; &lt;TextBlock Name="ErrorText" Foreground="White" Background="Red" FontSize="12" Padding="2" FontFamily="Trebuchet MS" TextWrapping="Wrap" Text="{Binding [0].ErrorContent}" &gt;&lt;/TextBlock&gt; &lt;StackPanel Margin="0" Orientation="Horizontal"&gt; &lt;Button Content="Create New" Command="{Binding Path=CreateNewFolder}" Margin="10" Padding="5"&gt;&lt;/Button&gt; &lt;Button Content="Cancel" Margin="10" Padding="5" &gt;&lt;/Button&gt; &lt;/StackPanel&gt; &lt;/StackPanel&gt; &lt;/Border&gt; &lt;AdornedElementPlaceholder Name="ErrorTextBox" /&gt; &lt;/DockPanel&gt; &lt;/ControlTemplate&gt; &lt;Style x:Key="ValidationStyle" TargetType="{x:Type ComboBox}"&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="Validation.HasError" Value="True"&gt; &lt;Setter Property="BorderBrush" Value="Red" /&gt; &lt;Setter Property="BitmapEffect"&gt; &lt;Setter.Value&gt; &lt;BitmapEffectGroup&gt; &lt;OuterGlowBitmapEffect GlowColor="Red" GlowSize="3" Noise="0.6"&gt;&lt;/OuterGlowBitmapEffect&gt; &lt;/BitmapEffectGroup&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;Setter Property="DataContext" Value="{Binding RelativeSource={x:Static RelativeSource.Self},Path=DataContext}"&gt; &lt;/Setter&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;AdornerDecorator &gt; &lt;ComboBox IsEditable="True" FontSize="11" Margin="10" Width="250" VerticalAlignment="Center" Text="{Binding Path=StrText, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" Validation.ErrorTemplate="{StaticResource validationTemplate}" Style="{StaticResource ValidationStyle}"&gt;&lt;/ComboBox&gt; &lt;/AdornerDecorator&gt; &lt;/Grid&gt; </code></pre> <p></p> <p><br> Please note that i set the DataContext property in the style:</p> <pre><code> &lt;Setter Property="DataContext" Value="{Binding RelativeSource={x:Static RelativeSource.Self},Path=DataContext}"&gt; &lt;/Setter&gt; </code></pre> <p><br><br> Please let me know how to bind the method to a button in the validation template. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload