我正在使用ContentPresenter(如MVC中的局部视图)在UserControl中显示(子)视图的简单列表,但数据未绑定。
尝试了数据绑定的详细版本,也尝试了一次划线,但没有数据绑定到显示。
UserControl具有一个名为WorkPieces的属性,该属性是WorkPieceViewModel的BindableCollection,并在启动时实例化为默认值,宽度:0,长度:0和指示符(罗马数字)。 UserControl视图的相关部分:
<ContentPresenter Grid.Row="7" Grid.Column="0" Grid.ColumnSpan="2" x:Name="WorkPieces" HorizontalAlignment="Stretch">
<ContentPresenter.ContentTemplate>
<DataTemplate DataType="{x:Type models:WorkPieceViewModel}">
<local:WorkPieceView />
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
WorkPieceView :(也是一个UserControl)
<DockPanel HorizontalAlignment="Stretch">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" x:Name="WorkPieceId" Visibility="Hidden" />
<TextBlock Grid.Row="0" Grid.Column="1" x:Name="Designator" Text="{Binding Designator}" MinWidth="15"/>
<TextBlock Grid.Row="0" Grid.Column="2" Text="{x:Static lang:Resources.Txt_W}" />
<TextBox Grid.Row="0" Grid.Column="3" x:Name="WorkWidth" MinWidth="50"/>
<TextBlock Grid.Row="0" Grid.Column="4" Text=" x " />
<TextBlock Grid.Row="0" Grid.Column="5" Text="{x:Static lang:Resources.Txt_L}" />
<TextBox Grid.Row="0" Grid.Column="6" x:Name="WorkLength" MinWidth="50"/>
<Button Grid.Row="0" Grid.Column="7" Margin="5">
<Image Source ="/Images/plus-sign.png" Height="16" Width="16" />
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cal:ActionMessage MethodName="AddNewWorkPiece" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Button Grid.Row="0" Grid.Column="8" Margin="5">
<Image Source ="/Images/minus-sign.png" Height="16" Width="16" />
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cal:ActionMessage MethodName="RemoveWorkPiece">
<cal:Parameter Value="{Binding ElementName=Id}" />
</cal:ActionMessage>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Grid>
</DockPanel>
主要问题是宽度和长度值未绑定。 我还有其他一些问题,例如Strech无法正常工作-但这只是UI,尽管其值是“ I”(罗马1),但指示器却没有显示。这可能是因为没有绑定发生。
答案 0 :(得分:0)
问题在于任务过于复杂。一个带有BindingList的简单ListBox解决了我的问题...