|
I'm starting with xaml created in a scene editor. This xaml is comprised of just the textblock definition. I thought the problem might be because the textblok isn't in a container of any kind, so I created a canvas, and put the textblock in it before trying to render it.
Here's my code:
private readonly XNamespace _xamlNS = @"http://schemas.microsoft.com/winfx/2006/xaml/presentation";
private readonly XNamespace _sysNS = @"clr-namespace:System;assembly=mscorlib";
private readonly XNamespace _xNS = @"http://schemas.microsoft.com/winfx/2006/xaml";
private readonly XNamespace _localNS = @"clr-namespace:System;assembly=WpfConsoleApp";
public void ImagesFromText2(string folder)
{
foreach (DynamicContentItem item in ContentItems)
{
if (item.ContentType == "text")
{
XElement container = new XElement("Canvas",
new XAttribute("xmlns", _xamlNS),
new XAttribute("xmlns:x", _xNS),
new XAttribute("xmlns:local", _localNS),
new XAttribute("Padding", "0,0,0,0"),
new XAttribute("Width", item.CanvasElement.Width.ToString()),
new XAttribute("Height", item.CanvasElement.Height.ToString()));
XElement child = item.CanvasElement.AsXElement;
string xaml = child.ToString();
child.Name = @"{http://clearchannel.com/spotchart/WpfConsoleApp}SpotChartTextBlock";
child.SetAttributeValue("Margin", "0,0,0,0");
child.SetAttributeValue("xmlns", _xamlNS);
child.SetAttributeValue("xmlns:x", _xNS);
child.SetAttributeValue("xmlns:local", _localNS);
string text = item.LocalData;
if (item.CanvasElement.IsDateTime)
{
child.SetAttributeValue("Text", item.LocalDataFormatted);
}
child.Name = @"{http://clearchannel.com/spotchart/WpfConsoleApp}SpotChartTextBlock.RenderTransform";
container.Add(child);
xaml = container.ToString();
Canvas canvas = (Canvas)(XamlReader.Parse(xaml));
SpotChartTextBlock textBlock = (SpotChartTextBlock)(canvas.Children[0]);
textBlock.VerticalTextAlignment = item.VerticalAlign;
textBlock.SizeToFit = item.SizeToFit;
textBlock.MinimumFontSize = item.MinimumFontSize;
textBlock.Process();
int width = (int)textBlock.Width;
int height = (int)textBlock.Height;
RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
DrawingVisual dv = new DrawingVisual();
using (DrawingContext ctx = dv.RenderOpen())
{
VisualBrush vb = new VisualBrush(canvas);
ctx.DrawRectangle(vb, null, new Rect(new Point(), new Size(width, height)));
}
rtb.Render(dv);
PngBitmapEncoder png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(rtb));
MemoryStream stream = new MemoryStream();
png.Save(stream);
System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
image.Save(System.IO.Path.Combine(folder, string.Format("{0}.png", item.ElementName)));
}
}
}
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
I don't think it needs a canvas. Does textBlock.Process() call the TextBlock.Measure() and TextBlock.Arrange()? I think that because you do not render to a UI, the values are not calculated, and these functions need to be explicitly called.
Jack of all trades ~ Master of none.
|
|
|
|
|
That was it. I called measure and arrange in the process method, and she wookee velly good.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
Good deal!
Jack of all trades ~ Master of none.
|
|
|
|
|
Source: http://www.fileplay.net/file/15671/highlightsearchedtextinlistview[^]
Hi,
Normally i'd try to strip code down to it's minimum and post it here, but this is one of those instances where the bare minimum = 6 files so please pardon the need to download
The Setup: Their's a listview with 3000 items with virtualization enabled in recycling mode. The ItemsSource property is bound to a collection of elements of type "Person". In the MainWindow i setup DataTemplates so the "Person" instances are represented by a custom control called "HighlightTextBlock" [it highlights portions of its text with a colored background if it matches its "HighlightPhrase" dependency property]. In MainWindow.xaml.cs the "FilterItems(...)" function uses a CollectionView to filter items so only ones with highlighted text are shown in the list.
The Issue: If you type some letters into the search box and then scroll down then you will see lots of items where the highlighting doesn't occur. Interestingly the problem doesn't occur if you enlarge the window to show more items.
The Hackish Fix: In HighlightTextBlock.cs, in the OnHighlightPhraseChanged(...) function [which fires when its associated dependency property "HighlightPhrase" changes], i found that the presence of this code "fixes" the problem:
tb.Text = tb.Text;
Btw trying things like
tb.InvalidateVisual(); had no effect here, and the HighlightPhraseProperty is using
FrameworkPropertyMetadataOptions.AffectsRender
The Crappy Fix: If VirtualizingStackPanel.VirtualizationMode="Standard" is used then the problem goes away (without using the Hackish Fix).
Interestingly this guy had issues while scrolling with recycling-virtualization mode that didn't manifest when recycling-standard mode was used:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a4792b31-0a0d-49e1-bcf1-62bb8e4e5326/[^]
Not Filtering With Collection Fix: The problem goes away if the contents of the "FilterItems(...)" function, in MainWindow.xaml.cs, is commented out so no CollectionView filtering is performed (without using the Hackish Fix).
So what i'm looking for is, maybe someone can explain why this behavior is occurring. Perhaps i'm doing something wrong etc.
|
|
|
|
|
Ok i figured it out. It has to do with the Text property. It gets changed when the items get recycled, when scrolling the list, and for some reason the "HighlightPhrase" property doesn't also get set when doing this and apparently this isn't an issue because the value of "HighlightPhrase" doesn't change when scrolling anyway. It just works, now
This is what makes it work (note: "new" keyword usage because this class inherits TextBlock):
public new string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public new static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string),
typeof(HighlightTextBlock), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsRender,
new PropertyChangedCallback(UpdateHighlighting)));
private static void UpdateHighlighting(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ApplyHighlight(d as HighlightTextBlock);
}
See article for latest code: Highlight Searched Text in WPF ListView[^]
modified 25-Jun-12 17:35pm.
|
|
|
|
|
When you add a TabControl in the VS 2010 designer, you get a TabItem added by default. (And a grid in the TabItem.) How can I duplicate this same type of effect in a custom control?
Interestingly, Expression Blend 4 will add two tabs to the TabControl by default.
Thank you for your answer.
|
|
|
|
|
All you have to do is add a UserControl to your project, add whatever controls you need, and then after you build your project, it will be available in the Toolbox, at the top under "yourProjectName" Controls. Then you can add this to any form, and it will automatically add all the other controls you added.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
Perhaps I wasn't clear on my request. I have already made a custom control & can add it via the toolbox. This control has content that can be added by the developer. This is like the TabControl - you can add TabItems and add your own controls in the TabItem.
The problem is that there is no initial clue for what/where to add your controls. The TabControl is nice because the designer pre-populates a TabItem for you.
How do you get the designer to pre-populate a non-default value for you? (By default, the TabControl has no items. But the designer adds one for you when you add it via the toolbox.)
Thank you for your response.
|
|
|
|
|
OK, I get it now. Unfortunately, I can't help you with that, sorry!
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
I want to communicate with serial port on client side system via asp pages.I think silverlight is the best solution,but there is no asp.net sample about this topic.
would you please tell me if there is a sample in this site or other web pages?
thanks
|
|
|
|
|
Silverlight is not the best option for what you are trying to do.
Silverlight runs in a sandboxed environment where accessing serial ports is not possible.
|
|
|
|
|
|
|
This site needs to sign in but I don’t know why I can’t sigup
|
|
|
|
|
Create a webservice on the server side that accepts the port data and a desktop app on the client side that reads the serial port and sends the data to the webservice.
Or; use automation (via MSHTML or a webbrowser control) to update the ASP page from a desktop app that reads the serial port.
Or; create a webservice on the client side that reads the serial port; a Siverlight app on the client side can then access the webservice to get port data.
|
|
|
|
|
Thanks for your answer.
Is there any sample about these ideas?
|
|
|
|
|
I gave you 3 options; you first need to decide which option you are going to use, and then break out it's components.
After that, there are plenty of examples out there (including on this site) to:
1) create a web service (server and/or client side)
2) read a serial port
3) browser automation using MSHTML
4) browser automation using the web browser component
5) calling javascript on a page and passing parameters
|
|
|
|
|
What hapened if there was a sample including this five issues together?
I'll try again to achieve my purpose.
But I hope I would make a component,when user connects to my website an activex download pops up and he accept it then every thing will be solved through internet borrowser.
Is that possible?i'm sure but it's too difficult for me.
|
|
|
|
|
First off, I wouldn't use an ActiveX control - that will limit you to PCs. Secondly, what exactly are you expecting from us?
|
|
|
|
|
Everything is possible (if you want it bad enough).
|
|
|
|
|
Thank you.
modified 29-Jun-12 6:16am.
|
|
|
|
|
But how does other commercial components work?like active comport.
|
|
|
|
|
Hello everyone
I have two images, image1 represent lunch and image2 represents dinner. When I run my application the image1 is displayed on main window.
How can I change image1 to image2 corresponding time of day (at run-time)?
For instance: lets say
from 12:00 to 17:00 I want my image1 to be displayed at main window, and
from 17:00 to 23:00 I want to display image2 at main window.
could someone please help me solve this problem, Hence: I'm new to WPF...
thanks in advance
kind regards
lapeci
|
|
|
|
|
Presumably you have a property representing the image to be displayed and bind your xaml to the property.
You then need a timer in your code (code behind or ViewModel). In the timers tick event you check the datetime.now and change the image property to the image you want to display.
Never underestimate the power of human stupidity
RAH
|
|
|
|