Click here to Skip to main content
15,881,852 members
Home / Discussions / WPF
   

WPF

 
QuestionRe: wpf Pin
ZurdoDev11-May-16 9:20
professionalZurdoDev11-May-16 9:20 
QuestionWPF Dependency Property Of Type UserControl Pin
Kevin Marois3-May-16 5:32
professionalKevin Marois3-May-16 5:32 
QuestionUse TTF Font From Resources Pin
Kevin Marois28-Apr-16 12:13
professionalKevin Marois28-Apr-16 12:13 
AnswerRe: Use TTF Font From Resources Pin
Richard Deeming29-Apr-16 2:18
mveRichard Deeming29-Apr-16 2:18 
GeneralRe: Use TTF Font From Resources Pin
Kevin Marois29-Apr-16 5:21
professionalKevin Marois29-Apr-16 5:21 
GeneralRe: Use TTF Font From Resources Pin
Kevin Marois29-Apr-16 5:26
professionalKevin Marois29-Apr-16 5:26 
GeneralRe: Use TTF Font From Resources Pin
Kevin Marois29-Apr-16 5:43
professionalKevin Marois29-Apr-16 5:43 
QuestionSave And Restore Attached Property Values Pin
Kevin Marois27-Apr-16 11:53
professionalKevin Marois27-Apr-16 11:53 
[UPDATE]... Sorry formatting isnt working

OVERVIEW
I'm working on a app where I need to be able produce a "report" which involves saving a piece of a view as a PNG and displaying it in Windows Photo Viewer. So I pass in the name of the topmost element and it creates the PNG and then opens the viewer. All works fine.

Now, I need to be able to exclude specific UI elements from appearing in the report. So I've written an Attached Property called ExcludeFromReport and applied it to multiple elements in my view.

Then in my class, PrintLib, I recurse the tree looking for the DP, and if it's set, I set its Visibility to Collapsed and produce the report. This work great because I can now "turn off" specific elements from appearing in the image.

PROBLEM
However.... when the property is set set to Collapsed it hides it in the UI at runtime also. So I need to find a way to store the value of the property, turn it off, then reset it. I'm an NOT looking for the default value. The default value may or may not be the value at runtime, so it can't be used.

POSSIBLE SOLUTION
Use a dictionary to hold the DP and its value before printing, then after the print routine is done, find it in the dict and reset it to its pre-print state.

CODE
Here's my class. See last method down "SetReportProperties"
public static class PrintLib
{
    // ======== DP'S
    public static DependencyProperty ExcludeFromReportProperty =
            DependencyProperty.RegisterAttached("ExcludeFromReport",
                                                typeof(bool),
                                                typeof(PrintLib),
                                                new PropertyMetadata(false));

<pre>
public static void SetExcludeFromReport(DependencyObject obj, bool value)
{
    obj.SetValue(ExcludeFromReportProperty, value);
}
public static bool GetExcludeFromReport(DependencyObject obj)
{
    return (bool)obj.GetValue(ExcludeFromReportProperty);
}

// ======== PUBLIC METHODS
public static void ViewImage(string filename)
{
    Process process = new Process();
    process.StartInfo.FileName = "rundll32.exe";
    process.StartInfo.Arguments = @"C:\WINDOWS\System32\shimgvw.dll, ImageView_Fullscreen " + filename;
    process.Start();
}

public static void GenerateReportFromControl(FrameworkElement element)
{
    PrepareReportElements(element);

    Rect rect = VisualTreeHelper.GetDescendantBounds(element);

    DrawingVisual dv = new DrawingVisual();

    using (DrawingContext ctx = dv.RenderOpen())
    {
        VisualBrush brush = new VisualBrush(element);
        ctx.DrawRectangle(brush, null, new Rect(rect.Size));
    }

    int width = (int)element.ActualWidth;
    int height = (int)element.ActualHeight;
    RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
    rtb.Render(dv);

    PngBitmapEncoder encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(rtb));

    string filename = @"C:\temp\ReportImage.jpg";
    using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None))
    {
        encoder.Save(fs);
    }

    ViewImage(filename);
}

// ======== PRIVATE METHODS
private static void PrepareReportElements(FrameworkElement element, int level = 0)

{
SetReportProperties(element);

var childCount = VisualTreeHelper.GetChildrenCount(element);

for (var i = 0; i <= childCount - 1; i++)
{
var visual = (FrameworkElement)VisualTreeHelper.GetChild(element, i);

SetReportProperties(visual);

if (VisualTreeHelper.GetChildrenCount(visual) > 0)
{
PrepareReportElements(visual, i);
}
}
}

private static void SetReportProperties(FrameworkElement element)
{
var excludeFromReportIsSet = element.ReadLocalValue(ExcludeFromReportProperty) != DependencyProperty.UnsetValue;

if (excludeFromReportIsSet)
{
var exclude = (bool)element.GetValue(ExcludeFromReportProperty);

if (exclude)
{
element.Visibility = Visibility.Collapsed;
}
}
}
}

If it's not broken, fix it until it is

AnswerRe: Save And Restore Attached Property Values Pin
Mycroft Holmes27-Apr-16 14:34
professionalMycroft Holmes27-Apr-16 14:34 
GeneralRe: Save And Restore Attached Property Values Pin
Kevin Marois28-Apr-16 6:47
professionalKevin Marois28-Apr-16 6:47 
AnswerRe: Save And Restore Attached Property Values Pin
Richard Deeming28-Apr-16 8:30
mveRichard Deeming28-Apr-16 8:30 
QuestionHow to have duplicate model/view-model in TabControl Pin
Leif Simon Goodwin21-Apr-16 2:01
Leif Simon Goodwin21-Apr-16 2:01 
AnswerRe: How to have duplicate model/view-model in TabControl Pin
Pete O'Hanlon21-Apr-16 3:57
mvePete O'Hanlon21-Apr-16 3:57 
GeneralRe: How to have duplicate model/view-model in TabControl Pin
Leif Simon Goodwin22-Apr-16 0:41
Leif Simon Goodwin22-Apr-16 0:41 
GeneralRe: How to have duplicate model/view-model in TabControl Pin
Pete O'Hanlon22-Apr-16 0:44
mvePete O'Hanlon22-Apr-16 0:44 
QuestionWPF App for video recording from webcam into file MP4/h.264 Pin
Qinitram13-Apr-16 23:33
Qinitram13-Apr-16 23:33 
AnswerRe: WPF App for video recording from webcam into file MP4/h.264 Pin
Gerry Schmitz17-Apr-16 19:31
mveGerry Schmitz17-Apr-16 19:31 
QuestionUserControl not displayed inside the parent window only in designer Pin
hesido9-Apr-16 0:18
hesido9-Apr-16 0:18 
QuestionLoading resources from XAML, problem with fonts Pin
Jayme655-Apr-16 10:17
Jayme655-Apr-16 10:17 
AnswerRe: Loading resources from XAML, problem with fonts Pin
Pete O'Hanlon5-Apr-16 21:49
mvePete O'Hanlon5-Apr-16 21:49 
QuestionCombox with Multi Column Header Pin
fratola29-Mar-16 8:08
fratola29-Mar-16 8:08 
AnswerRe: Combox with Multi Column Header Pin
Richard Deeming29-Mar-16 8:26
mveRichard Deeming29-Mar-16 8:26 
GeneralRe: Combox with Multi Column Header Pin
fratola31-Mar-16 20:26
fratola31-Mar-16 20:26 
Questionlistviewitem style Pin
caradri13-Mar-16 0:41
caradri13-Mar-16 0:41 
Questionxceed datagrid (community) and virtualization Pin
amigoface9-Mar-16 18:47
amigoface9-Mar-16 18:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.