내 생각에 MVVM 응용 프로그램에서보기 인쇄는 ViewModel의 책임이나 관심사가 아닙니다. 나는 당신이보기에서 이것을하는 것이 낫다고 믿습니다.
버튼에서 WPF 비헤이비어를 사용하는 방법 - 뷰에 DataTemplates를 사용하고 '코드 숨김'파일이 없기 때문에 비헤이비어를 사용합니다.
동작은 DependencyProperty를 노출합니다.이 속성은 인쇄 할 내용이나 인쇄 할 내용을 바인딩합니다.
XAML :
<Button Margin="0,2,5,2"
HorizontalAlignment="Right"
Content="PRINT"
ToolTip="Prints the current report">
<i:Interaction.Behaviors>
<b:ReportPrintClickBehavior Content="{Binding ElementName=SelectedReportContent, Mode=OneWay}" />
</i:Interaction.Behaviors>
</Button>
는 XAML의 행동을 참조하려면, 당신은 System.Windows.Interactivity를 참조해야합니다,이는 NuGet here에서 찾을 수 있습니다.
코드 숨김 (행동) :이 경우
나는 FlowDocument
가 FlowDocumentReader
내에서 호스팅 인쇄하고 있습니다.
public sealed class ReportPrintClickBehavior : Behavior<Button>
{
public static readonly DependencyProperty ContentProperty = DependencyProperty.Register("Content",
typeof(DependencyObject),
typeof(ReportPrintClickBehavior),
new PropertyMetadata(null));
public DependencyObject Content
{
get { return (DependencyObject)GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); }
}
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Loaded += OnLoaded;
AssociatedObject.Unloaded += OnUnloaded;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.Loaded -= OnLoaded;
AssociatedObject.Unloaded -= OnUnloaded;
}
private void OnLoaded(object sender, RoutedEventArgs args)
{
AssociatedObject.Click += OnClick;
}
private void OnUnloaded(object sender, RoutedEventArgs args)
{
AssociatedObject.Click -= OnClick;
}
private void OnClick(object sender, RoutedEventArgs args)
{
var flowDocumentReader = Content.GetVisualDescendent<FlowDocumentReader>();
if (flowDocumentReader != null)
{
flowDocumentReader.Print();
}
}
}
UI 인쇄는 ViewModel과 관련이 없습니다. 모든 UI 요소에 액세스 할 수있는 코드 숨김에 print 메서드를 구현하기 만하면됩니다. 끝난. – Will