在xaml MVVM模式中绑定进度条

时间:2014-11-21 15:41:22

标签: c# .net xaml mvvm

我正在尝试实现一个进度条,这样当我点击搜索按钮时,我需要弹出它。这就是我写的,但由于某些原因,它一直出现。任何回复都将非常感谢。

以下是我在xaml文档中的内容:

<ProgressBar Name="searchProgressBar" IsIndeterminate="True" Width="200" Margin="10,10,0,0" Visibility="{Binding IsSearchbarVisible, UpdateSourceTrigger=PropertyChanged}"/>

这就是我在VM.cs类中所拥有的:

private Visibility isSearchBarVisible = Visibility.Collapsed;
public Visibility IsSearchBarVisible
{
    get { return isSearchBarVisible; }
    set
    {
        isSearchBarVisible = value;
        OnPropertyChanged("IsSearchBarVisible");
    }
}

public ICommand OrderSearch
{
    get
    {
        if (_orderSearch == null)
        {
            _orderSearch = new RelayCommand(param =>
            {
                IsSearchBarVisible = Visibility.Visible;
                Console.WriteLine(isSearchBarVisible);
                int orderID;
                if(Int32.TryParse(param.ToString(), out orderID))
                    ShowMainOrderDetails(orderID);

                IsSearchBarVisible = Visibility.Collapsed;
                Console.WriteLine(isSearchBarVisible);
            });
        }
        return _orderSearch;
    }
}

2 个答案:

答案 0 :(得分:0)

绑定是xaml IsSearchbarVisible(b很小)和viewmodel IsSearchBarVisible(B是大写)中的问题

答案 1 :(得分:0)

请试试这个:

private ICommand _onOrderSearchClickCommand;
public ICommand OnOrderSearchClickCommand
{
    get
    {
        if (_onOrderSearchClickCommand != null) return _onOrderSearchClickCommand;
            _onOrderSearchClickCommand = new RelayCommand(OrderSearchButtonClick);
        return _onOrderSearchClickCommand;
    }
}
public void OrderSearchClick(object sender)
{
    IsSearchBarVisible = Visibility.Visible;
    Console.WriteLine(isSearchBarVisible);
    int orderID;
    if(Int32.TryParse(param.ToString(), out orderID))
        ShowMainOrderDetails(orderID);
}

并在你的方法结束时ShowMainOrderDetails(orderID)添加到最后

  

IsSearchBarVisible = Visibility.Hidden;