简体   繁体   English

如何使用子绑定中的父内容控件?

[英]How can I use a parent content control from a sub binding?

I have the following code currently: 我目前有以下代码:

<DataTemplate DataType="{x:Type vm:SectionViewModel}">
    <ScrollViewer>
        <ItemsControl ItemsSource="{Binding ViewModels}">
        </ItemsControl>
    </ScrollViewer>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:StringViewModel}">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
        </Grid.RowDefinitions>
       <Label Name="Left" Grid.Row="0" Grid.Column="0" Content="{Binding Label}"/>
       <TextBox Name="Right" HorizontalAlignment="Stretch" Grid.Row="0" Grid.Column="1" Text="{Binding Value}"/>
    </Grid>
</DataTemplate>

The ViewModels property bound to SectionViewModel ItemsControl is a list of StringViewModel. 绑定到SectionViewModel ItemsControl的ViewModels属性是StringViewModel的列表。 I want to insert each StringViewModel into some sort of content control in the ItemsControl. 我想将每个StringViewModel插入ItemsControl中的某种内容控件中。 Currently I just have each StringViewModel to make its own Grid, but that leaves things unaligned. 目前,我只有每个StringViewModel都可以创建自己的Grid,但是这使事情变得无法对齐。 I'd like to insert these items into some sort of content control in ItemsControl, it doesn't necessarily have to be a grid, but it should be within the ItemsControl. 我想将这些项目插入ItemsControl中的某种内容控件中,它不一定必须是网格,但它应该在ItemsControl中。 How can I do this? 我怎样才能做到这一点? I'm also following MVVM, using MVVM Light. 我也正在使用MVVM Light跟踪MVVM。

EDIT: I modified the XAML to reflect how I currently have it setup. 编辑:我修改了XAML,以反映当前如何设置它。

If you want to control the width in the containing template you can use an inherited attached property: 如果要控制包含模板中的宽度,可以使用继承的附加属性:

public class WidthInformation
{
   // Use propa snippet to create LabelWidth property with this metadata:
   ... RegisterAttached("LabelWidth", typeof(double), typeof(WidthInformation), new FrameworkPropertyMetadata
   {
     Inherits = true
   });
}

It would be used thusly: 因此,它将被使用:

<DataTemplate DataType="{x:Type vm:SectionViewModel}">      
  <ScrollViewer>      
    <ItemsControl ItemsSource="{Binding ViewModels}"
                  local:WidthInformation.LabelWidth="60" />
  </ScrollViewer>      
</DataTemplate>      
<DataTemplate DataType="{x:Type vm:StringViewModel}">      
  <DockPanel>
    <Label Content="{Binding Label}"
           Width="{Binding Path=(local:WidthInformation.LabelWidth)"/>      
    <TextBox Text="{Binding Value}"/>      
  </DockPanel>
</DataTemplate>

The use of a DockPanel will cause the TextBox width to automatically fill the remaining space. 使用DockPanel将导致TextBox宽度自动填充剩余的空间。

On the other hand, if you want the two columns to be the same percentage size relative to each other, you could use star sizing on the columns: 另一方面,如果希望两列相对于彼此具有相同的百分比大小,则可以在列上使用星号大小:

<DataTemplate DataType="{x:Type vm:StringViewModel}">      
  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="2*" />
      <ColumnDefinition Width="3*" />
    </Grid.ColumnDefinitions>
    <Label Content="{Binding Label}" />      
    <TextBox Text="{Binding Value}" Grid.Column="1" />      
  </DockPanel>
</DataTemplate>

A very simple solution is to hard-code the width inside the DockPanel instead of using an attached property: 一个非常简单的解决方案是在DockPanel内部硬编码宽度,而不是使用附加属性:

<DataTemplate DataType="{x:Type vm:StringViewModel}">      
  <DockPanel>
    <Label Content="{Binding Label}" Width="80" />
    <TextBox Text="{Binding Value}"/>      
  </DockPanel>
</DataTemplate>

Lastly, if you need the width to adjust based on label size you can use a grid with shared sizing: 最后,如果您需要根据标签大小调整宽度,则可以使用具有共享大小的网格:

<DataTemplate DataType="{x:Type vm:SectionViewModel}">      
  <ScrollViewer>      
    <ItemsControl ItemsSource="{Binding ViewModels}"
                  Grid.IsSharedSizeScope="true" />
  </ScrollViewer>      
</DataTemplate>      
<DataTemplate DataType="{x:Type vm:StringViewModel}">      
  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto" SharedSizeGroup="Label" />
      <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Label Content="{Binding Label}" />      
    <TextBox Text="{Binding Value}" Grid.Column="1" />      
  </DockPanel>
</DataTemplate>

WPF is just full of possibilities! WPF充满了可能性!

It will be easiest to change your DataTemplate (for SectionViewModel) to use a ListBox instead of your more generic controls there. 将您的DataTemplate(用于SectionViewModel)更改为使用ListBox代替那里的更通用的控件将是最简单的。 Once you have that set up, then change your DataTemplate to be attached to the ListBox. 设置完成后,请更改要附加到ListBox的DataTemplate。

Unfortunately, there's no really easy way to set up the controls like you are describing without getting into much more complex layouts. 不幸的是,如果不进入更复杂的布局,就没有真正简单的方法来设置您要描述的控件。 I would recommend setting it up like this, then if you need to bind to a certain width for your columns, attach it to your parent controls (or set up a property that determines the maximum width and have them all bind to that). 我建议这样设置,然后如果您需要为列绑定到某个宽度,请将其附加到父控件(或设置一个确定最大宽度并将其全部绑定到该属性的属性)。

    <ListBox ItemsSource="{Binding Path=ViewModels}">
        <ListBox.ItemTemplate>
            <DataTemplate DataType="{x:Type vm:StringViewModel}">
                <StackPanel Orientation="Horizontal">
                    <Label x:Name="Left" Content="{Binding Label}" Width="{Binding ElementName=SourceControlHere, Path=WidthToBindTo}"/>
                    <TextBox x:Name="Right" HorizontalAlignment="Stretch" Text="{Binding Value}" Width="{Binding ElementName=SourceControlHere, Path=WidthToBindTo2}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以使用DetailsView控件而不绑定吗? - Can I use the DetailsView control without binding? 如何通过子控件事件更改父控件的内容? - How can I change parent control's content though child control event? 使用ListView时,如何在Xamarin Forms中将自定义控件绑定设置为父视图模型属性? - How can I set custom control binding to parent view model property in Xamarin Forms when using a ListView? 我可以在Control的Content属性中使用“&lt; - ”符号吗? - Can I use sign “<-” in Content property of Control? 如何从子控件的resize事件调整父控件的大小? - How can I resize the parent control from a child control's resize event? 如何从父表单中的用户定义控件获取控件的值 - How can I get values of a control from the user-defined control in the parent form 如何在WPF中使用数据绑定为列表中的每个元素创建新的用户控件? - How can I use data binding in WPF to create a new user control for each element in a list? 如何在VisualTreeHelper.GetParent()更改时将属性附加到控件或使用会更新的绑定? - How can I attach a property to a control or use a binding that updates whenever VisualTreeHelper.GetParent() changes? 如何将WPF与XML绑定用于具有从兄弟姐妹到父级的相关数据的指定路径 - How do I use WPF binding with XML for a specified path with related data from siblings to the parent 从父控件绑定到子属性 - Binding to child properties from parent control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM