【问题标题】:Initializing a view in XAML with WPF (Caliburn.Micro)使用 WPF (Caliburn.Micro) 在 XAML 中初始化视图
【发布时间】:2017-11-14 15:06:52
【问题描述】:

我一直在尝试让工具提示在数据网格的每一行中工作。我可以使用简单的 StackPanel 和一些标签来显示 Tooltipproperty 中的信息,但现在我想插入一个视图以用作工具提示。

我能够显示 ciew 并且视图模型正在工作,但我无法使自定义对象工作(名为 AppointmentConfirmationNotification)。我可以使用空对象“ToolTipContent”,但我想将它绑定到 Datagrid。

这是我正在处理的代码。请注意,我在 Stackpanel 的“实验”中留下了评论。基本上我假设我必须以某种方式在某处插入一个“{Binding}”......但不确定在哪里。

<DataGrid.RowStyle>
                    <Style TargetType="DataGridRow">
                        <Style.Resources>
                            <model:AppointmentConfirmationNotification x:Key="ToolTipContent">
                            </model:AppointmentConfirmationNotification>

                        </Style.Resources>
                        <Setter Property="ToolTip">
                            <Setter.Value >
                                <!--
                                OLD STACKPANEL WORKING SAMPLE
                                <StackPanel>
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="*"></RowDefinition>
                                        </Grid.RowDefinitions>

                                        <WrapPanel Grid.Row="0">
                                            <Label>ID : </Label>
                                            <Label Content="{Binding AppointmentConfirmation.AppointmentID}"/>
                                        </WrapPanel>

                                    </Grid>
                                </StackPanel>
                                -->

                                <v:APTooltipView>
                                    <v:APTooltipView.DataContext>
                                        <ObjectDataProvider ObjectType="{x:Type vm:APTooltipViewModel}">
                                            <ObjectDataProvider.ConstructorParameters>
                                                <StaticResource ResourceKey="ToolTipContent"/>
                                            </ObjectDataProvider.ConstructorParameters>
                                        </ObjectDataProvider>
                                    </v:APTooltipView.DataContext>
                                </v:APTooltipView>
                            </Setter.Value>
                        </Setter>

                        <Setter Property="TextElement.FontWeight" Value="{Binding Path=Read,Converter={StaticResource BooleanToFontweight}}"/>
                    </Style>
                </DataGrid.RowStyle>

我试图在 XAML 上执行此操作,以便遵循更多的 MVVM 方法,但请随时建议另一种方法。谢谢

【问题讨论】:

    标签: c# wpf xaml mvvm datagrid


    【解决方案1】:

    您可以创建一个新视图并传递 DataContext。你可以把你喜欢的任何东西放在那个视图中。在您的主视图中应该是这样的。

    <StackPanel>
       <local:"YOUR_VIEW" DataContext="{Binding AppointmentConfirmation, Mode=TwoWay}"/>
    </StackPanel>
    

    您新制作的视图将如下所示。您可以添加任何您喜欢的内容(只要它存在于您的 DataContext 中),也可以将此视图重用于其他目的。

    <UserControl x:Name="YOUR_VIEW" ....
    ...... >
       <Grid>
         <Grid.RowDefinitions>
             <RowDefinition Height="*" />
         </Grid.RowDefinitions>
         <WrapPanel Grid.Row="0">
             <Label>ID : </Label>
             <Label Content="{Binding AppointmentID}"/>
         </WrapPanel>
       </Grid>
    </UserControl>
    

    【讨论】:

      猜你喜欢
      • 2010-11-19
      • 1970-01-01
      • 2011-12-14
      • 1970-01-01
      • 1970-01-01
      • 2020-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多