Wpf 如何将父属性绑定到子元素datatemplate

Wpf 如何将父属性绑定到子元素datatemplate,wpf,binding,datagrid,datatemplate,multidatatrigger,Wpf,Binding,Datagrid,Datatemplate,Multidatatrigger,如果DataGrids的子项通过转换器生成,我需要将DataGrid的RowStyle属性绑定到可见性。问题是子项存在于数据模板中。我下面的代码是我现在正在做的事情的简化版本,所以它可能没有多大意义。但无论如何: <DataGrid Name="dataGrid" ItemsSource="{Binding Path=ListOfData}" RowStyle="{StaticResource DataGridRowStyle}"

如果DataGrids的子项通过转换器生成,我需要将DataGrid的RowStyle属性绑定到可见性。问题是子项存在于数据模板中。我下面的代码是我现在正在做的事情的简化版本,所以它可能没有多大意义。但无论如何:

<DataGrid Name="dataGrid"
              ItemsSource="{Binding Path=ListOfData}"
              RowStyle="{StaticResource DataGridRowStyle}"
              >
        <DataGrid.RowDetailsTemplate>
              <DataTemplate>
                    <StackPanel>
                         <TextBlock Name="textBlock" Source={Binding Path=Title}
                         <Image Name="image" Source="{Binding Path=Image}"/>
                    </StackPanel>
              </DataTemplate>
        </DataGrid.RowDetailsTemplate>
  </DataGrid>


您确实不能在这里绑定到数据模板,因为它超出了RowStyle的范围

我会用另一种方式:

  • 在我看来,您应该能够在ItemViewModel中确定文本框和图像是否同时折叠
  • 将属性添加到ItemViewModel(例如“VisibilityProperty”),并在文本框和图像都折叠时将其设置为“折叠”
  • 通过RowStyle将DataGridRow的VisibilityProperty绑定到ItemViewModel的此属性
  • 这应该可以

    编辑: 如果无法更改itemViewModel,则可以使用其他选项(尽管非常难看):

    将行的可见性绑定到项本身,并使用转换器查找子项,查看它们是否在运行时折叠

    大致如下:

    <Style TargetType="{x:Type DataGridRow}" x:Key="DataGridRowStyle">
        <Setter Property="Visibility" Value="{Binding Converter=MyRowViewModelToVisibilityconverter}" />
    </Style>
    

    您确实不能在这里绑定到数据模板,因为它超出了RowStyle的范围

    我会用另一种方式:

  • 在我看来,您应该能够在ItemViewModel中确定文本框和图像是否同时折叠
  • 将属性添加到ItemViewModel(例如“VisibilityProperty”),并在文本框和图像都折叠时将其设置为“折叠”
  • 通过RowStyle将DataGridRow的VisibilityProperty绑定到ItemViewModel的此属性
  • 这应该可以

    编辑: 如果无法更改itemViewModel,则可以使用其他选项(尽管非常难看):

    将行的可见性绑定到项本身,并使用转换器查找子项,查看它们是否在运行时折叠

    大致如下:

    <Style TargetType="{x:Type DataGridRow}" x:Key="DataGridRowStyle">
        <Setter Property="Visibility" Value="{Binding Converter=MyRowViewModelToVisibilityconverter}" />
    </Style>
    

    您是否尝试在用户选择行时执行行扩展操作,在用户单击其他行时执行行折叠操作?您是否尝试在用户选择行时执行行扩展操作,在用户单击其他行时执行行折叠操作?我的问题是不允许我更改基础ItemModelView。但是你回答了我的问题,所以谢谢你。我必须进行一些重新设计,以使其正常工作。请参阅我编辑的答案,以了解在您的情况下可能会有帮助的解决方法。我的问题是不允许我更改基础ItemModelView。但是你回答了我的问题,所以谢谢你。我将不得不进行一些重新设计,以使这项工作。请参阅我编辑的答案,以解决您的情况,可能会有所帮助。
    public class MyRowViewModelToVisibilityconverter: MarkupExtension, IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var item = value as ItemViewModel;
            bool isImageCollapsed = false, isTextBoxCollapsed = false;
    
            // Look into your ItemViewModel for the properties inducing a Visibility=Collapsed on the Image and the TextBox
    
            return (isImageCollapsed && isTextBoxCollapsed) ? Visibility.Collapsed : Visbility.Visible;
    
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return new MyRowViewModelToVisibilityconverter();
        }
    }