开发者

Binding a subclass property

开发者 https://www.devze.com 2023-01-09 18:55 出处:网络
In WindowViewModel There is a property called statusBar In StatusBarViewModel , There is a property called

In WindowViewModel There is a property called statusBar

In StatusBarViewModel , There is a property called public BatteryIndicatorViewModel batIndicatorViewModel

In BatteryIndicatorViewModel , There is a property called public bool IsLowBattery

<MultiDataTrigger.Conditions>
<Condition Binding="{Binding statusBar.batIndicatorViewModel.IsLowBattery}" Value="true" />
 </MultiDataTrigger.Conditions>

IsLowBattery property is bound to CheckBox, in window2 and which is bound to WindowViewModel

<CheckBox IsChecked="{Binding Path=statusBar.batIndicatorViewModel.IsLowBattery}" Content="Low Battery" Grid.Column="2" Margin="0,0,34.743,14.04" Grid.Row="1" />

It shows the following error:- System.Windows.Data Error: 39 : BindingExpression path error: 'statusBar' property not found on 'object' ''StatusBarViewModel' (HashCode=25431989)'. BindingExpression:Path=statusBar.batIndicatorViewModel.IsLowBattery; DataItem='StatusBarViewModel' (HashCode=25431989); target element is 'ContentControl' (Name=''); target property is 'NoTarget' (type 'Object')


Code.....

Window2.xaml.. I have a HeaderedItemsControl in Window2.xaml which is bound to workspaces

<Grid>
<HeaderedItemsControl ItemsSource="{Binding Path=Workspaces}" Header="StatusBar" />
<CheckBox IsChecked="{Binding statusBar.batIndicatorViewModel.IsLowBattery}" Content="Low Battery" Grid.Column="2" Margin="0,0,34.743,14.04" Grid.Row="1" />
<CheckBox IsChecked="{Binding statusBar.batIndicatorViewMo开发者_运维问答del.IsCharging}" Content="Charging" Grid.Column="1" Grid.ColumnSpan="2" Height="15.96" Margin="5.76,0,48.67,0" Grid.Row="2" VerticalAlignment="Top" />
</Grid>

WindowViewModel.cs..

WindowViewModel which is bound to window2 datacontext

public class WindowViewModel:WorkspaceViewModel
{
public ObservableCollection<WorkspaceViewModel> Workspaces        

    public StatusBarViewModel StatusBarVM;

    private void ShowStatusBar()
            {            
                StatusBarVM = new StatusBarViewModel();
                this.Workspaces.Add(StatusBarVM);
                this.SetActiveWorkspace(StatusBarVM);            
            }

StatusBar.xaml..

<usercontrol>
<Grid Background="Black" Height="20" Width="240">
<vw:BatteryIndicator Height="20" Width="30" IsTabStop="False" VerticalAlignment="Top" HorizontalAlignment="Right" Grid.Column="1"/>
<!—some other usercontrols are used here -- >
</Grid>
</UserControl>

StatusBarViewModel.cs..

public class StatusBarViewModel : WorkspaceViewModel
    {
public BatteryIndicatorViewModel BatteryIndicatorVM;
}

BatteryIndicatorViewModel.cs..

public class BatteryIndicatorViewModel : WorkspaceViewModel
    {
Public  bool IsCharging;
public bool IsLowBattery;
}

Now my question is how do I bind IsCharging in BatteryIndicatorViewModel to low battery checkbox in windowviewmodel


The error is telling you that the DataContext of the element where the Binding is being used is already set to a StatusBarViewModel, most likely the statusBar property. You should be able to remove the "statusBar." from the beginning of your Binding Path and get what you're looking for.

0

精彩评论

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