Click here to Skip to main content
15,915,800 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: Datagrid is not updating the values after changing the collection in the ViewModel in WPF Pin
indian14313-May-16 21:09
indian14313-May-16 21:09 
GeneralRe: Datagrid is not updating the values after changing the collection in the ViewModel in WPF Pin
Pete O'Hanlon14-May-16 3:39
mvePete O'Hanlon14-May-16 3:39 
QuestionConvert This XAML To C# Pin
Kevin Marois12-May-16 13:45
professionalKevin Marois12-May-16 13:45 
QuestionAdd a check box and delete multiple rows from DataGrid in WPF Pin
indian14312-May-16 8:46
indian14312-May-16 8:46 
AnswerRe: Add a check box and delete multiple rows from DataGrid in WPF Pin
Mycroft Holmes12-May-16 13:06
professionalMycroft Holmes12-May-16 13:06 
GeneralRe: Add a check box and delete multiple rows from DataGrid in WPF Pin
indian14312-May-16 14:12
indian14312-May-16 14:12 
QuestionUse Different Paths In Control Template Pin
Kevin Marois11-May-16 13:14
professionalKevin Marois11-May-16 13:14 
AnswerRe: Use Different Paths In Control Template Pin
Gerry Schmitz11-May-16 16:06
mveGerry Schmitz11-May-16 16:06 
QuestionHandle Title Bar Icon Click Pin
Kevin Marois11-May-16 5:56
professionalKevin Marois11-May-16 5:56 
AnswerRe: Handle Title Bar Icon Click Pin
CHill6011-May-16 22:42
mveCHill6011-May-16 22:42 
AnswerRe: Handle Title Bar Icon Click Pin
Pete O'Hanlon11-May-16 23:50
mvePete O'Hanlon11-May-16 23:50 
Questionwpf Pin
Member 113037949-May-16 7:39
Member 113037949-May-16 7:39 
AnswerRe: wpf Pin
Pete O'Hanlon9-May-16 8:34
mvePete O'Hanlon9-May-16 8:34 
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 

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.