根据公共属性中设置的值(在WPF / XAML / MVVM Light应用程序中),我试图崩溃DataGridText列的想法已经用尽。
xaml的摘录是:
<StackPanel>
<DataGrid AutoGenerateColumns="False" Name="PipelinesGrid"
HorizontalAlignment="Left"
ItemsSource="{Binding Pipelines}"
SelectedItem="{Binding SelectedPipelineView, Mode=TwoWay}"
VerticalAlignment="Top" Margin="10,16,0,0"
SelectionUnit="FullRow" SelectionMode="Single"
CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn
Binding="{Binding DailyCapacity, Mode=TwoWay}"
Header="Capacity(d)"
Visibility="{Binding Source={x:Reference PipelinesGrid},
Path=DataContext.ShowCapacities}"/>
以下是Visibility设置是我尝试过的众多设置之一(这个提供了一个cycylical依赖错误)。
ViewModel代码的相关摘录是:
private string _showCapacities;
public string ShowCapacities
{
get { return _showCapacities; }
set { _showCapacities = value; RaisePropertyChanged("ShowCapacities"); }
}
private void OnProjectSelected(ProjectSelectedArgs obj)
{
_selectedProject = obj.SelectedProject;
if (_selectedProject != null)
{
Pipelines = new ObservableCollection<PipelineView>();
foreach (var projectPipeline in _selectedProject.ProjectPipelines)
{
Pipelines.Add(new PipelineView(projectPipeline));
}
switch (_selectedProject.ProjectCategory.ProjectCategory1)
{
case "Upstream":
ShowCapacities = "Collapsed";
break;
case "Mid-Stream":
ShowCapacities = "Visible";
break;
}
}
IsEditing = false;
}
我已经了解了这篇文章(http://stackoverflow.com/questions/8847661/datagridtextcolumn-visibility-binding),我需要使用Source和Xreference。谢谢你的帮助。
答案 0 :(得分:1)
有关SO的各种文章讨论了这个问题。搜索“wpf visibility binding datagrid column”。
问题是因为DataGrid列不属于DataGrid的可视树或逻辑树,因此WPF无法找到合适的DataContext。尝试使用亲属或元素绑定也会失败。
毋庸置疑,一些聪明的人已经解决了这个问题。我最喜欢的解决方案是这里解释的代理freezable方法:http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/对我来说很好。