绑定子类属性

时间:2010-07-30 10:13:06

标签: wpf mvvm

在WindowViewModel中 有一个名为statusBar

的属性

在StatusBarViewModel中,有一个名为的属性 public BatteryIndi​​catorViewModel batIndicatorViewModel

在BatteryIndi​​catorViewModel中,有一个名为的属性 公共布尔IsLowBattery

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

IsLowBattery属性绑定到CheckBox,在window2中绑定到WindowViewModel

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

它显示以下错误: - System.Windows.Data错误:39:BindingExpression路径错误:'object'''StatusBarViewModel'(HashCode = 25431989)'上找不到'statusBar'属性。 BindingExpression:路径= statusBar.batIndicatorViewModel.IsLowBattery; DataItem ='StatusBarViewModel'(HashCode = 25431989); target元素是'ContentControl'(Name =''); target属性是'NoTarget'(类型'Object')


代码.....

Window2.xaml .. 我在Window2.xaml中有一个绑定到工作空间的HeaderedItemsControl

<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.batIndicatorViewModel.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 ..

WindowSeModel绑定到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;
}

BatteryIndi​​catorViewModel.cs ..

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

现在我的问题是我如何将BatteryIndi​​catorViewModel中的IsCharging绑定到windowviewmodel中的低电量复选框

1 个答案:

答案 0 :(得分:0)

错误告诉您正在使用Binding的元素的DataContext已经设置为StatusBarViewModel,很可能是statusBar属性。您应该能够删除“statusBar”。从你的Binding Path开始,得到你想要的东西。