用户控制重构 - 我可以做得更好吗?
我想使用下面的用户控件进行三种不同的演示。因为传递的数据上下文中的对象具有公共接口,所以每个演示文稿中唯一会有所不同的两件事是:
- 图像资源(“imgDepartment”)
- FilterPanel 控件(EmployeeFilterPanel)
事实上,只是因为这些两个项目,我需要该用户的三个独立版本,其中大部分是重复的代码。
我可以这样做吗?如何?
干杯,
Berryl
<DockPanel LastChildFill="True" >
<uc:ListSubjectHeader DockPanel.Dock="Top"
Subject="{Binding PresentationSubject}"
AddNewItemCommand="{Binding AddCommand}"
ImageSource="{StaticResource imgDepartment}"
/>
<local:EmployeeFilterPanel DockPanel.Dock="Top" DataContext="{Binding MasterVm}"/>
<Grid Margin="0, 5, 0, 0">
<common:MasterListingWorkspacesControl />
</Grid>
</DockPanel>
更新
通过设置样式并向我的 ViewModel 添加PresentationImageUri 属性来解决图像部分,如下所示:
<Style x:Key="ListSubjectHeaderStyle" TargetType="{x:Type uc:ListSubjectHeader}">
<Setter Property="DockPanel.Dock" Value="Top" />
<Setter Property="Subject" Value="{Binding PresentationSubject}" />
<Setter Property="AddNewItemCommand" Value="{Binding AddCommand}" />
<Setter Property="ImageSource" Value="{Binding PresentationImageUri}" />
</Style>
仍然不确定如何处理 FilterPanel 类型。
I would like to use the User Control below for three different presentation. Because the objects that are in the passed data context have common interfaces, the only two things that will vary from each presentation will be:
- the image resource ("imgDepartment")
- the FilterPanel control (EmployeeFilterPanel)
As it is though, just because of these two items, I need three separate versions of this User with mostly duplicated code.
Can I do that? How?
Cheers,
Berryl
<DockPanel LastChildFill="True" >
<uc:ListSubjectHeader DockPanel.Dock="Top"
Subject="{Binding PresentationSubject}"
AddNewItemCommand="{Binding AddCommand}"
ImageSource="{StaticResource imgDepartment}"
/>
<local:EmployeeFilterPanel DockPanel.Dock="Top" DataContext="{Binding MasterVm}"/>
<Grid Margin="0, 5, 0, 0">
<common:MasterListingWorkspacesControl />
</Grid>
</DockPanel>
Update
Solved the image part by setting up a style and adding a PresentationImageUri property to my ViewModel as below:
<Style x:Key="ListSubjectHeaderStyle" TargetType="{x:Type uc:ListSubjectHeader}">
<Setter Property="DockPanel.Dock" Value="Top" />
<Setter Property="Subject" Value="{Binding PresentationSubject}" />
<Setter Property="AddNewItemCommand" Value="{Binding AddCommand}" />
<Setter Property="ImageSource" Value="{Binding PresentationImageUri}" />
</Style>
Still not sure how to deal with that FilterPanel type yet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不设置ImageSource &通过代码隐藏的DataContext?
Why not set the ImageSource & DataContext through codebehind?
我讨厌回答自己的问题,但也许这会对别人有所帮助。我最终在视图模型中结合使用了 DataTemplates、资源转换器和字符串键。更多详细信息和一些代码此处
B
I hate to answer my own stuff but maybe it will help someone else. I wound up using a combination of DataTemplates, a resource converter and a string key in my view model. More details and some code here
B