0

我有一个带有绑定的按钮,ClearCommand它可以清除文本框的值,但它不起作用。

public SellerDetailsViewModel()    //constructor
    {
        sdObject = new SellerDetailsTable();
        _SaveCommand = new RelayCommand(Save, CanSave);
        _ClearCommand = new RelayCommand(clear, Canclear);
    }
private readonly ICommand _ClearCommand;
public ICommand ClearCommand { get { return _ClearCommand; } }
public event PropertyChangedEventHandler PropertyChanged;
    public void onPropertyChange(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

public bool Canclear(object obj)
{
    return true;
}

public void clear(object obj)
{
    this.PancardNumber = "";
    this.ContactNumber = 0;
    this.FirstName = "";
    this.LastName = "";
    this.MiddleName = "";
    this.OtherDocument = "";
    this.Address = "";            
}

<Page x:Class="CarDealer.SellerDetails"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
mc:Ignorable="d" 
d:DesignHeight="800" d:DesignWidth="900"
xmlns:ViewModels="clr-namespace:CarDealer.PresentationLayer.ViewModel"
Title="SellerDetails">
<Page.Resources>
<ViewModels:SellerDetailsViewModel x:Key="ViewModel">
</ViewModels:SellerDetailsViewModel>
<ControlTemplate x:Key="TextBoxErrorTemplate">
<DockPanel LastChildFill="True">
<TextBlock DockPanel.Dock="Bottom" Foreground="Red" FontSize="10pt" 
Text="{Binding ElementName=MyAdorner,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"/>
<Border BorderBrush="Red" BorderThickness="2" Width="225" Height="35" >
<AdornedElementPlaceholder Name="MyAdorner" />
</Border>
</DockPanel>
</ControlTemplate>
</Page.Resources>


<Grid DataContext="{Binding Source={StaticResource ViewModel}}">
    <Label Content="Seller Details" Height="39" HorizontalAlignment="Left" Margin="27,25,0,0" Name="lblTitle" VerticalAlignment="Top" FontSize="22" FontWeight="Bold" FontFamily="Times New Roman" Foreground="#FF1313D8" Width="169" />
    <Label Content="First Name" Height="28" HorizontalAlignment="Left" Margin="44,87,0,0" Name="lblCustName" VerticalAlignment="Top" FontSize="16" />
    <Label Content="Address" FontSize="16" Height="28" HorizontalAlignment="Left" Margin="461,253,0,0" Name="lblAdress" VerticalAlignment="Top" />
    <Label Content="Pan Card Number" FontSize="16" Height="28" HorizontalAlignment="Left" Margin="44,243,0,0" Name="lblPanCardNumber" VerticalAlignment="Top" />
    <Label Content="Contact Number" FontSize="16" Height="28" HorizontalAlignment="Left" Margin="44,320,0,0" Name="lblContactNumber" VerticalAlignment="Top" />
    <Label Content="Handover Date" FontSize="16" Height="28" HorizontalAlignment="Left" Margin="461,170,0,0" Name="label1" VerticalAlignment="Top" />
    <Label Content="Other Document" FontSize="16" Height="28" HorizontalAlignment="Left" Margin="44,399,0,0" Name="label2" VerticalAlignment="Top" />
    <Button Content="Save" Height="37" Command="{Binding SaveCommand}" HorizontalAlignment="Left" Margin="299,543,0,0" Name="btnSave" VerticalAlignment="Top" Width="131" FontSize="22" />
    <TextBox Height="33" Text="{Binding FirstName, Mode=TwoWay}" HorizontalAlignment="Left" Margin="205,92,0,0" Name="txtFirstName" VerticalAlignment="Top" Width="225" BorderThickness="2" FontSize="16" />
    <Label Content="Last Name" FontSize="16" Height="28" HorizontalAlignment="Left" Margin="44,165,0,0" Name="label3" VerticalAlignment="Top" />
    <TextBox BorderThickness="2" Text="{Binding LastName, Mode=TwoWay}" Height="33" HorizontalAlignment="Left" Margin="205,170,0,0" Name="txtLastName" VerticalAlignment="Top" Width="225" FontSize="16" />
    <TextBox BorderThickness="2" Text="{Binding PancardNumber, Mode=TwoWay}" Height="33" HorizontalAlignment="Left" Margin="205,248,0,0" Name="txtPanCard" VerticalAlignment="Top" Width="225" FontSize="16" />
    <TextBox BorderThickness="2"  Height="33" HorizontalAlignment="Left" Margin="205,325,0,0" Validation.ErrorTemplate="{StaticResource TextBoxErrorTemplate}" Name="txtcontactNumber" VerticalAlignment="Top" Width="225" FontSize="16">
        <TextBox.Text>
            <Binding Path="ContactNumber">
                <Binding.ValidationRules>
                    <ExceptionValidationRule/>
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>
    <TextBox Height="126" Text="{Binding Address, Mode=TwoWay}" HorizontalAlignment="Left" Margin="627,248,0,0" Name="TxtAddress" VerticalAlignment="Top" Width="257" BorderThickness="2" FontSize="16" VerticalScrollBarVisibility="Visible" />
    <TextBox BorderThickness="2" Text="{Binding OtherDocument, Mode=TwoWay}" Height="126" HorizontalAlignment="Left" Margin="205,399,0,0" Name="rchTxtOtherDoc" VerticalAlignment="Top" Width="257" FontSize="16" VerticalScrollBarVisibility="Visible" />
    <DatePicker Height="33" Text="{Binding HandoverDate}" HorizontalAlignment="Left" Margin="627,170,0,0" Name="dtPickerHandoverDate" VerticalAlignment="Top" Width="225" FontSize="16" />
    <Button Content="Clear All" Command="{Binding ClearCommand}"  FontSize="22" Height="37" HorizontalAlignment="Left" Margin="500,543,0,0" Name="btnClearAll" VerticalAlignment="Top" Width="131" />
    <Label Content="Middle Name" FontSize="16" Height="28" HorizontalAlignment="Left" Margin="471,87,0,0" Name="label4" VerticalAlignment="Top" />
    <TextBox BorderThickness="2" Text="{Binding MiddleName, Mode=TwoWay}" FontSize="16" Height="33" HorizontalAlignment="Left" Margin="627,87,0,0" Name="txtMiddleName" VerticalAlignment="Top" Width="225" />
</Grid>

我已经调试了代码执行。clear函数已执行,但在视图中,文本框不为空。你能帮我弄清楚我做错了什么吗?

我已经添加了剩余的代码,请问缺少什么。

4

2 回答 2

2

您应该发布其余的 xaml。假设您的控件是 TextBox 的...

public void clear(object obj)
{
    this.PancardNumber.Text = "";
    this.ContactNumber.Text = "0"; //depends what this control is
    this.FirstName.Text = "";
    this.LastName.Text = "";
    this.MiddleName.Text = "";
    this.OtherDocument.Text = "";
    this.Address.Text = "";            
}
于 2013-09-26T10:10:35.183 回答
0

试试这个,希望它有帮助。

public void clear(object obj)
{
    this.PancardNumber = "";
    this.ContactNumber = 0;
    this.FirstName = "";
    this.LastName = "";
    this.MiddleName = "";
    this.OtherDocument = "";
    this.Address = "";   
    // should work on other document
    OnPropertyChanged("OtherDocument");
}


    <Button Content="Clear All" Command="{Binding ClearCommand}"  FontSize="22" Height="37" HorizontalAlignment="Left" Margin="500,543,0,0" Name="btnClearAll" VerticalAlignment="Top" Width="131" />
    <TextBox BorderThickness="2" Text="{Binding OtherDocument, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="126" HorizontalAlignment="Left" Margin="205,399,0,0" Name="rchTxtOtherDoc" VerticalAlignment="Top" Width="257" FontSize="16" VerticalScrollBarVisibility="Visible" />
于 2013-09-26T10:18:35.937 回答