breezback
asked on
WPF Change data context on the fly
Hi,
problem:
Let's say I have trying to render the same wpf window and pass different data context one after the other for example a counter.
For example:
Window that shows counter.
I need to see the counter increment 1, then 2
var counter = new Counter(1);
DataContext = counter;
//force update & refresh
counter = new Counter(2);
DataContext = counter;
//force update & refresh
I'm new to WPF how can I do it? Do I need depency? or to fire and event?
Please submit full sample.
problem:
Let's say I have trying to render the same wpf window and pass different data context one after the other for example a counter.
For example:
Window that shows counter.
I need to see the counter increment 1, then 2
var counter = new Counter(1);
DataContext = counter;
//force update & refresh
counter = new Counter(2);
DataContext = counter;
//force update & refresh
I'm new to WPF how can I do it? Do I need depency? or to fire and event?
Please submit full sample.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
How is this handled then? Do you have a timer or something? Can you provide some sample code.
ASKER
Let's say I have a command line that sends the counter "10"
The WPF should render 10 times, each time showing the counter.
For each render I need to do a snapshot with this code that is working:
As you can see, my DataContext is called Counter - and I need to catch each Render event to produce 10 snapshots. I don't mind to use a Timer but this counter can also be very big (500)
The WPF should render 10 times, each time showing the counter.
For each render I need to do a snapshot with this code that is working:
private static void ExportAsImage(Fr<wbr ></wbr><wbr ></wbr>ameworkEle<wbr ></wbr>ment element, Counter counter)
{
element.DataContext = counter;
var target = new RenderTargetBitmap((int)(7<wbr ></wbr><wbr ></wbr>20), (int)(620), 96, 96, PixelFormats.Pbgra32);
target.Render(element);
var encoder = new PngBitmapEncoder();
var outputFrame = BitmapFrame.Create(target)<wbr ></wbr><wbr ></wbr>;
encoder.Frames.Add(outputF<wbr ></wbr><wbr ></wbr>rame);
var source = Guid.NewGuid().ToString("N<wbr ></wbr><wbr ></wbr>").Substri<wbr ></wbr>ng(0, 20) + ".png";
using (var file = File.OpenWrite(source))
{
encoder.Save(file);
//throw new Exception("done");
}
}
As you can see, my DataContext is called Counter - and I need to catch each Render event to produce 10 snapshots. I don't mind to use a Timer but this counter can also be very big (500)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Need the Timer to make it work
ASKER
Thanks for your comment, my program doesn't have any buttons that are triggered.
How can I do it with no user interaction at all.