开发者

WPF TabControl Binding issue

开发者 https://www.devze.com 2023-04-06 08:19 出处:网络
I have TabControl with two tabs: main time and additional time. Main view model contains main hours an开发者_高级运维d additional hours as properties, first tab bound to main hours, second tab - to ad

I have TabControl with two tabs: main time and additional time. Main view model contains main hours an开发者_高级运维d additional hours as properties, first tab bound to main hours, second tab - to additional hours. Each hour has a clear command (DelegateCommand from Prism). On the top of TabControl there is a button with command "Clear all" (CompositeCommand). Hours are creating in the constructor of main view model.

Issue:

Commands from additional hours are not exists cause of and they are not participate in command "Clear all", and button "Clear all" is not enabled until I select second tab (in the first tab is nothing to clear, but on the secons there is).

I tried to disabled virtualizing in TabControl but it didn't help me. Also after loading data I call clearCommand.RaiseCanExecuteChanged for each hour.

Update: When I iterate through hours in debugger and watch if command can execute it's all fine and UI updates properly. But without debugger it's not.


I'm not sure I understand you correctly, but there are two things I can think of that might cause unusual behavior in your design

  1. Prism's DelegateCommand does not automatically re-query CanExecute if a parameter changes. To verify if this is your problem or not, I'd switch to using a RelayCommand of some kind.... I often use Galasoft's MVVM Light RelayCommand when I want a command to automatically re-evaluate it's CanExecute when a parameter changes

  2. By default, WPF unloads TabItems that are not visible. So if the SaveAllButton.Enabled property is based off of Tab1.SaveButton.Enabled and Tab2.SaveButton.Enabled, Tab2 has something to save, but Tab1 does not, and Tab1 has focus, then the SaveAll button will be disabled because Tab2 is not visible and therefore, not loaded. To test if this is the case, switch to using something like a ListBox. If it is the case, you can use the code found here to keep your TabControl from unloading non-visible tabs.

0

精彩评论

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

关注公众号