无法将XAML按钮可见性与c#视图模型私有可见性变量绑定
本文关键字:可见性 模型 绑定 变量 视图 XAML 按钮 | 更新日期: 2023-09-27 18:01:51
我想将XAML文件中的按钮可见性绑定到相关c#视图模型代码中的私有可见性变量。当代码运行时,按钮继续显示,而不是像私有可见性变量中定义的那样隐藏。在网上看到很多类似问题的回复后,我尝试了很多方法。
在输出窗口中看到的以下运行时错误已停止产品开发:
System.Windows。数据错误:40:BindingExpression路径错误:'SaveCalButtonVisibility'属性在'object' 'CalibrationSet' (HashCode=40040273)'上找不到。BindingExpression:路径= SaveCalButtonVisibility;DataItem = ' CalibrationSet ' (HashCode = 40040273);目标元素是'Button' (Name= ");目标属性为"可见性"(类型为"可见性")
当前对象是CalibrationSetList
,由于某种原因,它似乎试图将XAML保存校准按钮可见性属性绑定到DataItem=CalibrationSet Path=SaveCalButtonVisibility
路径。我相信这应该绑定到DataItem=CalibrationSetList Path=SaveCalButtonVisibility
路径正常工作。我不知道如何完成这个绑定。
开发环境是Microsoft Visual Studio 2010 with Infragistics 2013.2。XAML和c#代码如下所示,并省略了不相关的代码。代码是按照MVVM模式、WPF和PRISM 4.0编写的
任何帮助、建议或继续对话都是非常感谢的。
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
}
}
假设你的CalibrationSetList绑定到一个CalibrationSetListViewModel(这可能不是,很难告诉)的问题将是不同的DataContexts之间的UserControl(这将是CalibrationSetListViewModel)和网格中的单元格(将是网格绑定到的集合中的单个项)。
要将这些属性(默认绑定到集合中各个项目的属性)绑定到顶层ViewModel,请使用:
{Binding RelativeSource={RelativeSource AncestorType=DataGrid},
Path=DataContext.SaveCalButtonVisibility }
希望有帮助