开发者

Can we have buttons in a validation template, and how can we bind it to the ViewModel method?

开发者 https://www.devze.com 2023-02-01 09:47 出处:网络
Requirement: Need to display an error message when the user types a forlder name that doesn\'t exist as shown below:

Requirement:

Need to display an error message when the user types a forlder name that doesn't exist as shown below:

Can we have buttons in a validation template, and how can we bind it to the ViewModel method?

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"

View Model 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 => this.OnCreateNewFolder());
            return createNewFolder;

        }

    }
    public void OnCreateNewFolder()
    {
        MessageBox.Show("CreateNewFolder");
    }

RelayCommand.cs can be downloaded at: http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=mag200902MVVM&DownloadId=4357

Xaml Code:

<Window.Resources>        
    <ControlTemplate x:Key="validationTemplate">
        <DockPanel LastChildFill="True">
            <Border Margin="5,5,0,0" DockPanel.Dock="Bottom" Background="Red">
            <StackPanel>
                <TextBlock Name="ErrorText"  Foreground="White" Background="Red" 
                                   FontSize="12" Padding="2" FontFamily="Trebuchet MS"                                                                                
                                   TextWrapping="Wrap"                                        
                                   Text="{Binding [0].ErrorContent}" ></TextBlock>
                <StackPanel Margin="0" Orientation="Horizontal">
                    <Button Content="Create New" Command="{Binding Path=CreateNewFolder}" Margin="10" Padding="5"></Button>
                    <Button Content="Cancel" Margin="10" Padding="5" ></Button>
                </StackPanel>
            </StackPanel>
            </Border>
            <AdornedElementPlaceholder Name="ErrorTextBox" />
        </DockPanel>
    </ControlTemplate>
    <Style x:Key="ValidationStyle" TargetType="{x:Type ComboBox}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="True">
                <Setter Property="BorderBrush" Value="Red" />
                <Setter Property="BitmapEffect">
                    <Setter.Value>
                        <BitmapEffectGroup>
                            <OuterGlowBitmapEffect GlowColor="Red" GlowSize="3" Noise="0.6"></OuterGlowBitmapEffect>
                        </BitmapEffectGroup>
                    </Setter.Value>
                </Setter> 
                <Set开发者_运维问答ter Property="DataContext"
                        Value="{Binding RelativeSource={x:Static RelativeSource.Self},Path=DataContext}">
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>


<Grid>
    <AdornerDecorator >
        <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}"></ComboBox>
    </AdornerDecorator>
</Grid>

Please note that i set the DataContext property in the style:

 <Setter Property="DataContext" Value="{Binding RelativeSource={x:Static 
          RelativeSource.Self},Path=DataContext}">
 </Setter>    

Please let me know how to bind the method to a button in the validation template.


You could reference the DataContext for the AdornerDecorators Child in the Binding. I think something like this will work

<Button Content="Create New"
        Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type AdornerDecorator}},
                          Path=Child.DataContext.CreateNewFolder}"
        Margin="10" Padding="5"></Button>


This line looks suspicious:

createNewFolder = new RelayCommand(param => this.OnCreateNewFolder());

Maybe you should replace it by:

createNewFolder = new RelayCommand(OnCreateNewFolder);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号