简体   繁体   English

使用WPF(Caliburn.Micro)在XAML中初始化视图

[英]Initializing a view in XAML with WPF (Caliburn.Micro)

I've been trying to get a tooltip to work inside of each row of a datagrid. 我一直在尝试获取工具提示以在datagrid的每一行中工作。 I was able to displaythe info inside the Tooltip property using a simple StackPanel and some labels, but now I wanted to insert a view to use as a tooltip. 我可以使用简单的StackPanel和一些标签在Tooltip属性中显示信息,但是现在我想插入一个视图以用作工具提示。

I was able to display the ciew and the viewmodel is working, yet I cannot make the custom object to work(named AppointmentConfirmationNotification ). 我能够显示ciew,并且viewmodel在工作,但是我无法使自定义对象工作(名为AppointmentConfirmationNotification )。 I am able to use the empty object 'ToolTipContent' yet I want to bind it to the Datagrid. 我可以使用空对象'ToolTipContent',但我想将其绑定到Datagrid。

here is the code I'm cureently working on. 这是我正在努力工作的代码。 Be aware that I left a comment on my working Stackpanel 'experiment'. 请注意,我对正在使用的Stackpanel“实验”发表了评论。 Basically I assume somehow I have to inser a mere ´{Binding}´ somewhere... But not sure where. 基本上,我认为我必须在某个地方插入一个简单的“ {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>

I was trying to do this on the XAML in order to follow a more MVVM approach, but feel free to advice another approach. 我试图在XAML上执行此操作,以遵循更多的MVVM方法,但是随时建议另一种方法。 Thank you 谢谢

You can just make a new view and pass the DataContext through. 您可以创建一个新视图并传递DataContext。 You could put whatever you like in that view. 您可以在该视图中放入您喜欢的任何内容。 It would look like this in your main view. 在您的主视图中看起来像这样。

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

Your freshly made view will look like this. 您新创建的视图将如下所示。 You can add whatever you like (as long as it exists in your DataContext) and maybe reuse this view for other purposes. 您可以添加任何您喜欢的内容(只要它存在于您的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>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM