【问题标题】:WPF passing a bool to the view and changing itWPF 将布尔值传递给视图并更改它
【发布时间】:2013-08-05 15:34:37
【问题描述】:

如何将视图模型中的布尔值传递给视图并更改值。 例如,在我的视图模型中,我创建了一个 bool

public bool load
{
    get { return m_load; }
    set
    {
        m_load = value;
        OnPropertyChanged(this, o => o.load);
    }
}

然后我有我的SelectedVm 代码

public ViewModel SelectedVm
{
    get { return _selectedVm; }
    set
    {        
        _selectedVm = value;
        if (_selectedVm != null && load == true)
        {
            _selectedVm.Load();                   
        }
        Load = false;
        OnPropertyChanged(this, o => o.SelectedVm);               
    }
}

在我看来,SelectedVm 被绑定了两次,但我只希望在其中一个绑定上调用 Load,因此需要更改布尔值 load

所以在我看来,如果我有以下情况

<ListView Grid.Row="1"  Name="Sample"  
          ItemsSource="{Binding Path=SampleViewModel}"  
          SelectedItem="{Binding SelectedVm, Mode=TwoWay}"     
          IsSynchronizedWithCurrentItem="True" Width="500">            
</ListView>

如何将 bool 负载更改为 true 或 false 以上所有只是快速示例,我认为这可能很简单,但是我不习惯WPF,并且仍在学习。任何建议都会很棒

【问题讨论】:

  • 为什么要从viewmodel传递一个bool到view并改变值?
  • 我有一个问题,方法 Load() 被多次调用但它只需要调用一次,在我看来 SelectedVM 被绑定了两次,这意味着 Load() 被调用了两次,但我只想要一次,所以每次绑定 SelectedVm 时都会设置 bool。希望这是有道理的
  • 你的代码应该只调用一次 Load(),即使你有多个绑定。问题是什么? – Liel 5 分钟前
  • 另一个绑定是新视图的 dataContext 绑定,我原以为这意味着它被调用了两次,而且它肯定是因为我已经逐步完成了代码。

标签: c# .net wpf data-binding mvvm


【解决方案1】:

好的,如果你想在 View 中获取 Load 的值并且你想在纯 MVVM 模式中执行它,那么在 View 中创建 bool 类型的 DependencyProperty 并将其绑定到 VM 的 Load 属性,如

public partial class MainWindow : Window
{
    public static readonly DependencyProperty LoadProperty = DependencyProperty.Register("MyCustom", typeof(bool), typeof(MainWindow), new PropertyMetadata(new PropertyChangedCallback(LoadPropertyChangedCallback)));

    public bool Load
    {
        get
        {
            return (bool)this.GetValue(LoadProperty) ;
        }
        set
        {
            this.SetValue(LoadProperty, value);
        }
    }

    static void LoadPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    { 
        //Do your load stuff here
    }

    public MainWindow()
    {
        InitializeComponent();
        this.SetBinding(LoadProperty, new Binding("load"));
        DataContext = new ViewModel();
    }
}
public class ViewModel : INotifyPropertyChanged
{
    public ViewModel()
    {
        load = true;
    }
    bool m_load;
    public bool load
    {
        get { return m_load; }
        set
        {
            m_load = value;
            OnPropertyChanged("load");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
}

【讨论】:

  • 你能告诉我如何在视图代码 xaml 中使用它吗?谢谢
【解决方案2】:

您发布的代码确保Load() 被调用一次,即使有多个绑定。

如果我正确理解了您的真正问题,您实际上是在寻求一种方法来确保每个 SelectedVM 都调用一次 Load() 函数,而且只调用一次。对吧?
如果是这样,您需要将 bool 属性添加到 ViewModel 类,而不是主类,仅此而已。

然后:

public ViewModel SelectedVm
{
    get { return _selectedVm; }
    set
    {        
        _selectedVm = value;
        if (_selectedVm != null && _selectedVm.load == true)
        {
           _selectedVm.Load();                   
           _selectedVm.load = false;
        }
        OnPropertyChanged(this, o => o.SelectedVm);               
    }
}

您可以保持 XAML 原样。

【讨论】:

  • 好吧,想象一下我有一个对象列表,每个对象都有另一个子对象列表,当我运行程序时,第一个对象被选中,并且它的子对象在给定之后显示在屏幕上time the sub objects for that object are refreshed this calls load for the first time (this is fine), then when a new object is selected this should call load only once but for some reason it calls it twice, i have put this down to绑定多次调用它
  • 我的猜测是您正在创建两个ViewModel 对象,而您看到的是在这两个对象上调用的setter,当然包含两个不同的Load 属性。如果您真的需要我们的帮助,您将需要发布您的真实代码。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-24
  • 2018-04-09
  • 2015-09-08
  • 1970-01-01
  • 1970-01-01
  • 2012-05-09
  • 1970-01-01
相关资源
最近更新 更多