|
Ok, that now compiled. When I run it, I get:
Triggers collection members must be of type EventTrigger.
Not even sure what that means.
If it's not broken, fix it until it is
|
|
|
|
|
Unfortunately, you can only use triggers like that in a Style. So define a Style for the TextBlock, and put your triggers in there.
(Yes, it's very annoying)
|
|
|
|
|
You need to call a DataTrigger from inside a style. Now, what you could do is set the style up so that you set the FontWeight normally, and then you would only have to apply the condition to change the weight. You don't actually need both conditions, because styles always revert back to their defaults when the trigger condition is no longer met.
|
|
|
|
|
Ok, I'v been working with this, but I can't get it quite right.
I have a disabled list which acts as a table of contens for a series of 'pages'. When a page becomes active, the TOC item for it needs to be bold.
So here's my list:
<ListBox Grid.Row="1"
Grid.Column="0"
Width="225"
ItemsSource="{Binding TOC}"
BorderBrush="SlateBlue"
BorderThickness=".5"
Padding="10"
IsEnabled="False">
<ListBox.Resources>
<DataTemplate DataType="{x:Type models:TOCModel}">
<StackPanel Orientation="Horizontal"
Margin="2">
<TextBlock Text="{Binding Caption}">
</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.Resources>
</ListBox>
and here's my style:
<Style TargetType="{x:Type ListBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}">
<Grid Width="Auto"
Height="Auto">
<Border x:Name="Border"
BorderBrush="SlateBlue"
BorderThickness=".5"/>
<ScrollViewer Focusable="false"
IsTabStop="False"
Margin="8"
HorizontalScrollBarVisibility="Disabled">
<StackPanel IsItemsHost="true"/>
</ScrollViewer>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Background" Value="White"/>
</Style>
The list is bound to a collection of TOCModels:
public class TOCModel
{
public string Caption { get; set; }
public bool IsCurrentItem { get; set; }
}
I think I need a DataTemplate for the TOC Items to set the FontWeight to bold when IsCurrentItem = true, but I don't know where to add it.
Could use some help.
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
Add the following style:
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="FontWeight" Value="Normal" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsCurrentItem}" Value="True">
<Setter Property="FontWeight" Value="Bold" />
</DataTrigger>
</Style.Triggers>
</Style> This will get applied to all ListBoxItem elements and set the font weight appropriately.
|
|
|
|
|
how i can create graph in wpf
santy143all
|
|
|
|
|
By writing some code. Seriously, this is not a good question, if you paste this into Google you will get lots of suggestions which you can at least learn from.
|
|
|
|
|
There are a number of third party tools that you could use to implement charts.
The WPF / Silverlight toolkit itself has its own set of chart controls that you can use.
|
|
|
|
|
|
I have ViewModel that has implement IDataErrorInfo interface. Binding is working fine but Now I have a requirement that error Template should not be shown until user lost focus through the Text Box. Currently when form Loaded all the fields are validated and showing red border.
WANTED wasim khan(Killed 50 Innocent Buggs, Distroyed 200 Exception, make 5 Project Hostage) any Compnay Hire him will pay 30,000. Best place where u can get him is Sorcim Technologies Murre Road RWP
|
|
|
|
|
You could toggle a boolean variable that is set when you are loading data and reset once this is done.
Only allow the validation code to run when the state of the bool is false.
E.g.
If (!load)
{
}
|
|
|
|
|
I'm working on some code that will ultimately be used in a Windows service, but right now it's in a console app for easier development.
I want to render a WPF TextBlock control to an image. I'm trying to use RenderTargetBitmap , and it's creating a png file on the expected dimensions, but the file is empty (well, it's 2K in size, but there's nothing in it).
Can anyone tell me how to do what I'm trying to do?
".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
|
|
|
|
|
After reading your question, I did a search and came across this article[^].
Maybe wrapping the visual into a VisualBrush and then painting the DrawingContext is what you are looking for?
The author has attached a small sample program as well.
|
|
|
|
|
Well, that example assumes a complete WPF app, which isn't what I'm doing (remember, I'm doing a console app/windows service), but I'll see if it works...
".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
|
|
|
|
|
No change.
".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
|
|
|
|
|
*Groan* I honestly thought this fix would help you.
I wonder what the issue is?
|
|
|
|
|
I was honestly hoping it would.
".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
|
|
|
|
|
John, without the WPF rendering subsystem being involved, you won't have the necessary infrastructure in place to render out your bitmap. You are missing a Dispatcher , which means that you cannot allocate a MediaContext instance.
|
|
|
|
|
I don't want to actually see the control, I just want to render it to an image, and save it as a PNG. Should I create a WPF class library and use it from my console app? This code will ultimately be located in a Windows service.
".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'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.
|
|
|
|
|