我想将XAML文件中的按钮可见性绑定到关联的C#视图模型代码中的私有Visibility变量。运行代码时,按钮将继续显示,而不是隐藏在私有可见性变量中。在网上阅读了许多类似问题的回复后,我尝试了很多东西。
在输出窗口中看到的以下运行时错误已停止产品开发:
System.Windows.Data错误:40:BindingExpression路径错误:'object'''CalibrationSet'(HashCode = 40040273)'上找不到'SaveCalButtonVisibility'属性。 BindingExpression:路径= SaveCalButtonVisibility; DataItem ='CalibrationSet'(HashCode = 40040273); target元素是'Button'(Name ='');目标属性是“可见性”(类型“可见性”)
当前对象是 CalibrationSetList ,由于某种原因,它似乎尝试将XAML保存校准按钮可见性属性绑定到 DataItem = CalibrationSet Path = SaveCalButtonVisibility 路径。我相信这应该绑定到 DataItem = CalibrationSetList Path = SaveCalButtonVisibility 路径才能正常工作。我无法完成这种约束。
开发环境是带有Infragistics 2013.2的Microsoft Visual Studio 2010。 XAML和C#代码如下所示,不相关的代码被省略。代码遵循MVVM模式,WPF和PRISM 4.0
编写非常感谢任何帮助,建议或持续对话。
Ken Schmitt
XAML代码:
<!-- CalibrationSetList.xaml -->
<UserControl
x:Class="Method.Control.CalibrationSetList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:I18N="clr-namespace:I18N;assembly=I18N"
xmlns:I18NStrings="clr-namespace:I18N.Strings;assembly=I18N.Strings"
xmlns:Conv="clr-namespace:Converters;assembly=Converters"
mc:Ignorable="d"
xmlns:Local="clr-namespace:Method.Control"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:AuthInfrastructure="clr-namespace:Authorization.Components;assembly=Authorization.Components"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid Grid.Row="0" Visibility="{Binding Path=CalibrationSetListControl}">
<Grid Visibility="{Binding Path=CalibrationSetListVisibility}" >
<Grid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
</Grid.Resources>
<DataGrid EnableRowVirtualization="False"
EnableColumnVirtualization="False"
IsReadOnly="True"
ItemsSource="{Binding CalibrationCollection, Mode=TwoWay}"
SelectedValue="{Binding Path=SelectedCalibrationSetValue}"
HorizontalGridLinesBrush="Transparent"
VerticalGridLinesBrush="Transparent" Margin="30">
<DataGrid.Columns>
<!--…………………………………->
<!--Save Calibration button-->
<DataGridTemplateColumn Width="125">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Style="{DynamicResource CustomButton}"
Visibility="{Binding Path=SaveCalButtonVisibility,Mode=TwoWay,
diag:PresentationTraceSources.TraceLevel=High}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Command="{Binding Path=DataContext.SaveCalibrationClick,
RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type DataGrid}}}"
CommandParameter="{Binding}" >
<Button.Content>
<MultiBinding Converter="{Conv:StringToLocalizedString ResourceKey=SaveCalibration}">
<Binding RelativeSource="{RelativeSource AncestorType=UserControl}"
Path="DataContext.SelectedLanguage"></Binding>
</MultiBinding>
</Button.Content>
<i:Interaction.Behaviors>
<AuthInfrastructure:ControlBehavior x:Name="btnCalibrationSetSave"
VMBounded="CalibrationSetSave" ModuleName="Calibration" />
</i:Interaction.Behaviors>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
<!--…………………………………->
<!--Calibration Set Control-->
<Grid Visibility="{Binding Path=CalibrationSetControl}" HorizontalAlignment="Left" Width="762">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Local:CalibrationSet
Grid.Row="0"
Grid.Column="0"
DataContext="{Binding CalibrationSetViewModelContext,Mode=TwoWay}"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto">
</Local:CalibrationSet>
</Grid>
<!--End Calibration Set Control-->
</Grid>
</UserControl>
匹配C#代码:
// CalibratonSetListViewModel.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using DataStructures;
using Framework;
using Method.Component;
using Method.Control.Calibration.Interface;
using Microsoft.Practices.Prism.Regions;
using EventAggregation;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.ServiceLocation;
using ViewModel.Base;
using Microsoft.Practices.Prism;
using CustomMessageBox;
using Chromatogram.DataDisplay.Control;
namespace Method.Control
{
/// <summary>
/// Set or Get CalibrationSetViewModelContext
/// </summary>
public class CalibrationSetListViewModel : ViewModelBase
{
#region Variables
//………………………………
/// <summary>
/// Save Cal Button Visibility
/// </summary>
private Visibility m_SaveCalButtonVisibility;
//………………………………
#endregion
#region Properties
//………………………………
/// <summary>
/// Set or Get Save Cal Button Visibility
/// </summary>
public Visibility SaveCalButtonVisibility
{
get
{
return m_SaveCalButtonVisibility;
}
set
{
m_SaveCalButtonVisibility = value;
OnPropertyChanged(() => SaveCalButtonVisibility);
}
}
//………………………………
#endregion
#region Constructor
/// <summary>
/// Initializes new instance of MethodListViewModel
/// </summary>
/// <param name="methodId">MethodId</param>
/// <param name="peakDataProvider">PeakDataProvider</param>
public CalibrationSetListViewModel(int methodId, IPeakDataProvider peakDataProvider)
{
try
{
m_DataDisplayOfflineViewModel = new DataDisplayOfflineViewModel(true); // gain access to member functions
m_MethodId = methodId;
m_PeakDataProvider = peakDataProvider;
m_CalibrationSetListVisibility = Visibility.Visible;
m_CreateButtonVisibility = Visibility.Collapsed;
m_SaveCalButtonVisibility = Visibility.Collapsed;
GetCalibrationsList();
CreateCalibrationClick = new RelayCommand(parm => this.CreateCalibration());
DeleteCalibrationClick = new RelayCommand(this.DeleteCalibration);
SaveCalibrationClick = new RelayCommand(this.SaveCalibration);
CalibrationDetailsClick = new RelayCommand(this.CalibrationDetails);
CalibrationResultClick = new RelayCommand(this.CalibrationResult);
//Publish menu clicked event
m_EventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
MenuHomePage menuHomePage = MenuHomePage.CalibrationSet;
m_EventAggregator.GetEvent<ToolBarChange>().Publish(menuHomePage);
SetCalibrationVisibility();
//Subscribe CreateNewCalibration method
m_CreateNewCalibration = m_EventAggregator.GetEvent<ToolBarCreateNewCalibrationEvent>();
m_CreateNewCalibrationToken = m_CreateNewCalibration.Subscribe(createNewCalibration);
}
catch (Exception)
{
throw;
}
}
#endregion
#region Public Methods
//………………………………
#endregion
#region Public Methods
//………………………………
#endregion
#region Private Methods
/// <summary>
/// Create New CalibrationSet
/// </summary>
/// <returns></returns>
private Entities.CalibrationSet CreateNewCalibrationSet()
{
Entities.CalibrationSet newCalibrationSet = new Entities.CalibrationSet();
newCalibrationSet.CalibrationSetId = 0;
newCalibrationSet.CalibrationSetNumber = 1;
newCalibrationSet.AmountUnits = 2;
return newCalibrationSet;
}
//………………………………
#endregion
}
}
答案 0 :(得分:0)
假设您的CalibrationSetList绑定到CalibrationSetListViewModel(可能不是,很难说),问题将是UserControl(可能是CalibationSetListViewModel)之间的不同DataContexts和 网格中的单元格(将是网格绑定到的集合中的单个项目)。
要将这些属性(默认情况下绑定到集合的各个项的属性)绑定到顶级ViewModel,请使用:
{Binding RelativeSource = {RelativeSource AncestorType = DataGrid},Path = DataContext.SaveCalButtonVisibility}
希望有所帮助