WPF ColumnDataPoint Annotation partial visible - c#

I have chart with column series in my application. I'm using solution from this tutorial to add annotation on top of columns:
http://blogs.msdn.com/b/delay/archive/2009/07/27/simple-column-labels-you-can-create-at-home-re-templating-the-silverlight-wpf-data-visualization-columndatapoint-to-add-annotations.aspx
When the bar is really tall the top part of the annotation is only partial visible, or not visible.
I do not know range of my data, so I can not set Maximum value on the vertical Axis.
How to solve this problem?

Like Andre said, the problem is your Margin, so you could use Multibinding to calculate the Margin like so:
<ControlTemplate TargetType="charting:ColumnDataPoint">
<Grid>
<Rectangle Name="clmnRectangle"
Fill="{TemplateBinding Background}"
Stroke="Black"/>
<Grid Background="#aaffffff"
HorizontalAlignment="Center"
VerticalAlignment="Top">
<Grid.Margin>
<MultiBinding Converter="{StaticResource ResourceKey=HeightToMargin}">
<Binding Path="ActualHeight" RelativeSource="{RelativeSource AncestorType={x:Type charting:ColumnDataPoint}}"></Binding>
<Binding Path="ActualHeight" RelativeSource="{RelativeSource AncestorType={x:Type charting:ColumnSeries}}"></Binding>
</MultiBinding>
</Grid.Margin>
<TextBlock Name="tbValue"
Text="{TemplateBinding FormattedDependentValue}"
FontWeight="Bold"
Margin="2"/>
</Grid>
</Grid>
</ControlTemplate>
The Converter could look like:
public class HeightToMarginConverter:IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo ci)
{
double clmnHeight = System.Convert.ToDouble(values[0]);
double chrtHeight = System.Convert.ToDouble(values[1]);
if (chrtHeight - clmnHeight < 20)
{
return new Thickness(0, clmnHeight - chrtHeight + 5, 0, 0);
}
else
{
return new Thickness(0, -20, 0, 0);
}
}
public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo ci)
{
return null;
}
}

Related

Aligning my Popup to bottom of Window

im trying to align my Popup to the bottom center of my Window, how ever i'm getting this error:
Additional information: Specified cast is not valid.
This is being caused by my converter double windowWidth = (double)values[0];, how ever the ActualWidth should bee a double! Not too sure on what is going wrong here.
I'm currently showing the data in a MessageBox just to test it at the moment and make sure the values look correct.
Converter
namespace Test_Project.Converters
{
public class NotificationOffsets : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double windowWidth = (double)values[0];
double notificationWidth = (double)values[1];
MessageBox.Show("Notification Width: " + notificationWidth.ToString() + " Window Width: " + windowWidth.ToString());
return false;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
XAML - Converter Binding
<Style TargetType="Popup" x:Key="PopupNotification">
<Setter Property="IsOpen" Value="True" />
<Setter Property="HorizontalOffset">
<Setter.Value>
<MultiBinding Converter="{StaticResource NotificationOffsets}">
<Binding RelativeSource="{RelativeSource Self}" Path="PlacementTarget.ActualWidth" />
<Binding RelativeSource="{RelativeSource Self}" Path="ActualWidth" />
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
Edit:
Breakpoint Data:
Edit 2:
I have now set my PlacementTarget within my Style:
<Setter Property="PlacementTarget" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" />
Still getting the same error!
You have to return a double on your converter, you are returning a boolean, which cause the invalid cast.
EDIT: You are having Binding problems, you have to set the "PlacementTarget" of your popup in order the get the Width property.
EDIT 2: try this:
<Window x:Class="WpfApplication7.MainWindow"
Name="myWindow"
............
<Setter Property="PlacementTarget" Value="{Binding ElementName=myWindow}"/>

Positioning of Axis Label in a DateTimeAxis

At the moment I have a date time axis where the date is in-line with the points, is there anyway to get this date to appear in the center such as on a bar chart.
<Style x:Key="DateTimeAxisLabelStyle2" TargetType="chartingToolkit:DateTimeAxisLabel">
<Setter Property="DaysIntervalStringFormat" Value="{}{0:dd-MMM}" />
<Setter Property="HoursIntervalStringFormat" Value="{}{0:hh:mm tt}" />
<!--<Setter Property="RenderTransformOrigin" Value="1,0.5" />
<Setter Property="RenderTransform">
<Setter.Value>
<RotateTransform Angle="-45" />
</Setter.Value>
</Setter>-->
<!--<Setter Property="Margin" Value="30,0,-10,0" />-->
</Style>
<chartingToolkit:DateTimeAxis IntervalType="Days"
Interval="1"
Minimum="{Binding StartDate}"
Maximum="{Binding EndDate}"
Orientation="X"
VerticalContentAlignment="Center"
Title="Day"
AxisLabelStyle="{StaticResource DateTimeAxisLabelStyle2}" />
Any help would be greatly appreciated.
Here's what i got:
XAML:
<Window.Resources>
<Style x:Key="DateTimeAxisLabelStyle1" TargetType="{x:Type chartingToolkit:DateTimeAxisLabel}">
<Setter Property="DaysIntervalStringFormat" Value="{}{0:dd-MMM}"></Setter>
<Setter Property="RenderTransformOrigin" Value="0.80,0.20"></Setter>
<Setter Property="RenderTransform">
<Setter.Value>
<RotateTransform Angle="-90"></RotateTransform>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<chartingToolkit:Chart Margin="0" Title="Chart Title">
<chartingToolkit:Chart.DataContext>
<local:MyDataCollection/>
</chartingToolkit:Chart.DataContext>
<chartingToolkit:Chart.Axes>
<chartingToolkit:DateTimeAxis Minimum="{Binding StartDate}" Maximum="{Binding EndDate}" Orientation="X" ShowGridLines="True" AxisLabelStyle="{DynamicResource DateTimeAxisLabelStyle1}"/>
</chartingToolkit:Chart.Axes>
<chartingToolkit:LineSeries DependentValuePath="Y" IndependentValuePath="X" ItemsSource="{Binding}"/>
</chartingToolkit:Chart>
</Grid>
Chart:
Here's what I did using the WPF Toolkit Source for reference.
I created a custom class deriving from DateTimeAxis, then overrode the "GetPlotAreaCoordinate" method. The DateTimeAxis.Render() calls that method three times with the same list of "DateTime" values, once for the MajorTickmarks, once for MinorTickmarks, and once for the date label. There were no minor tickmarks in the list, so the method was actually only getting called twice. I just keep a list of the values that have been evaluated and assume that if it's in the list it's already done the tickmarks and is now doing the Labels.
class CustomDateTimeAxis : DateTimeAxis
{
List<object> _valueList = new List<object>();
UnitValue prevBaseValue;
protected override UnitValue GetPlotAreaCoordinate(object value, Range<IComparable> currentRange, double length)
{
_valueList.Add(value);
UnitValue baseValue = base.GetPlotAreaCoordinate(value, currentRange, length);
int valueCount = _valueList.Count((x) => x.Equals(value));
if (valueCount == 2)
return new UnitValue(baseValue.Value + 27, baseValue.Unit);
prevBaseValue = baseValue;
return baseValue;
}
protected override void Render(Size availableSize)
{
base.Render(availableSize);
_valueList.Clear();
}
}
"27" is just a number I was trying out. You might want to play with that to see what works best for you.
return new UnitValue(baseValue.Value + 27, baseValue.Unit);
I created a Margin-Converter:
public class MarginConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var dateTimeAxis = values[0] as DateTimeAxis; ;
var actualAxisLength = values[1] as double?;
var actualMaximum = values[2] as DateTime?;
var actualMinimum = values[3] as DateTime?;
if (dateTimeAxis == null ||
!dateTimeAxis.Interval.HasValue ||
!actualAxisLength.HasValue ||
!actualMaximum.HasValue ||
!actualMinimum.HasValue)
return null;
double xMargin = 0;
var interval = dateTimeAxis.Interval.Value;
var timeSpan = actualMaximum.Value - actualMinimum.Value;
var timeSpanInDays = timeSpan.TotalDays;
if (dateTimeAxis.IntervalType == DateTimeIntervalType.Months)
{
xMargin = 30 * interval * actualAxisLength.Value / timeSpanInDays;
}
else if (dateTimeAxis.IntervalType == DateTimeIntervalType.Days)
{
xMargin = interval * actualAxisLength.Value / timeSpanInDays;
}
return new Thickness(xMargin, 10, 0, -30);
}
public object[] ConvertBack(object value, System.Type[] targetType, object parameter, CultureInfo culture)
{
return null;
}
}
called the X-Axis 'SharedXAxis' and used the converter like this:
<Setter Property="Margin">
<Setter.Value>
<MultiBinding Converter="{StaticResource MarginConv}">
<Binding ElementName="SharedXAxis"/>
<Binding ElementName="SharedXAxis" Path="ActualWidth"/>
<Binding ElementName="SharedXAxis" Path="ActualMaximum"/>
<Binding ElementName="SharedXAxis" Path="ActualMinimum"/>
</MultiBinding>
</Setter.Value>
Imo this should be full dynamic.
The Top- and Bottom-Values of the Thickness of the Margin-Converter ('10' and '-30' in my case) as well as the Bottom-Value of the Padding of the Chart itself have to be adjusted, I don't know why.

WPF Binding from strings to Canvas objects in a ResourceDictionary using Converter

I have a ResourceDictionary made up of icons/Canvas objects drawn with Paths. My ViewModel includes a string property (IconName) that contains a string matching one of the entries in the ResourceDictionary. I developed a MultiBinding (IMultiValueConverter) that takes the string, and a FrameworkElement and does a resource lookup, returning the resource matching the name. Before getting to this point, I stubbed my View explicitly with the following:
<Rectangle Width="10" Height="10" Margin="0,0,10,0">
<Rectangle.Fill>
<VisualBrush Stretch="Fill" Visual="{StaticResource defalt_icon}" />
</Rectangle.Fill>
</Rectangle>
This renders correctly. However, when I switch out to the following, nothing is rendered in the Rectangle.
<Rectangle Width="10" Height="10" Margin="0,0,10,0">
<Rectangle.Fill>
<VisualBrush Stretch="Fill">
<VisualBrush.Visual>
<MultiBinding Converter="{StaticResource IconNameConverter}">
<MultiBinding.Bindings>
<Binding RelativeSource="{RelativeSource AncestorType=FrameworkElement}"/>
<Binding Path="IconName"/>
</MultiBinding.Bindings>
</MultiBinding>
</VisualBrush.Visual>
</VisualBrush>
</Rectangle.Fill>
</Rectangle>
My converter (show below) is being called, and does find the Canvas object and returns it (viewing the object in the debugger I can see that Canvas has a Path child that has the right Data member filled in).
public class IconNameConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
FrameworkElement targetElement = values[0] as FrameworkElement;
string iconName = values[1] as string;
if (iconName == null)
return null;
FrameworkElement newIcon = (FrameworkElement)targetElement.TryFindResource(iconName);
if (newIcon == null)
newIcon = (FrameworkElement)targetElement.TryFindResource("appbar_page_question");
return newIcon;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Any ideas why the canvas isn't showing up?

Why does only one out of two multibindings work?

I am using a MultiBinding in two different points in my XAML. Here is the code:
<StatusBarItem>
<StackPanel Orientation="Horizontal">
<TextBlock Text="X " />
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource CoordinateToStringConverter}" TargetNullValue="-">
<Binding Path="ChartMouseX" />
<Binding Path="AxisSettingsViewModel.XAxisSettings.LabelFormat" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<TextBlock Text=" Y " />
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource CoordinateToStringConverter}" TargetNullValue="-">
<Binding Path="ChartMouseY" />
<Binding Path="AxisSettingsViewModel.YAxisSettings.LabelFormat" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</StatusBarItem>
I am facing a weird issue. The first MultiBinding works perfectly, but the second one is never called. If I comment out the first MultiBinding, the second one starts to work as expected.
Is this some kind of limitation in WPF? Or am I missing something about multibindings?
P.S: The RaisePropertyChanged is correctly invoked. However, in the second binding the converter does not get called at all.
EDIT
Here is the code of the Converter:
using System;
using System.Globalization;
using System.Windows.Data;
namespace LogViewer.Converters
{
public class CoordinateToStringConverter : IMultiValueConverter
{
#region IMultiValueConverter members
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values != null && values.Length == 2)
{
object value = values[0];
string format = values[1].ToString();
if (value is DateTime)
return ((DateTime)value).ToString(format);
if (value is TimeSpan)
return ((TimeSpan)value).ToString();
if (value is double)
return ((double)value).ToString(format);
}
return null;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
#endregion
}
}
I have analyzed your issue. Which is working fine for me. Nothing wrong with the multibinding.
Have you checked the converter with breakpoint was it called two times. Otherwise your problem is in ChartMouseY and ChartMouseX.

Two-way binding requires path or xpath

I want to increase Progress-bar value based on two textbox's Text. I wrote this XAML but there is an error "Two-way binding requires path or xpath" when I do MultiBinding in ProgressBar.Value
<Window.Resources>
<local:Class1 x:Key="ConverterM"/>
</Window.Resources>
<TextBox Height="23" HorizontalAlignment="Left" Margin="157,59,0,0"
Name="textBox1" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="157,108,0,0"
Name="textBox2" VerticalAlignment="Top" Width="120" />
<ProgressBar Height="24" HorizontalAlignment="Left" Margin="120,160,0,0"
Name="progressBar1" VerticalAlignment="Top" Width="243" >
<ProgressBar.Value>
<MultiBinding Converter="{StaticResource ConverterM}">
<Binding />
<Binding ElementName="textBox1" Path="Text" />
<Binding ElementName="textBox2" Path="Text" />
</MultiBinding>
</ProgressBar.Value>
</ProgressBar>
Value Converter:
public class Class1 : IMultiValueConverter
{
public object Convert(object[] values,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
if (values[1] != null && values[2]!=null)
{
if (((string)values[1]).Length==((string)values[2]).Length)
{
return 5.0;
}
}
else
{
return 0.0;
}
}
public object[] ConvertBack(object value,
Type[] targetTypes,
object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
I think that <Binding /> is not necessary. try to delete it and change indexes in converter.
Two-way binding requires path or xpath
This happens when you haven’t set the Path= on binding. By default WPF binding will take the Path= part by default.
To avoid this you need to give Path for each Binding you specify in MultiBinding. here in your case there was an empty binding which has no Path defined thats why you have experience with the above error.
I have came across the same issue but the accepted answer does not say what the error is, So thought of sharing this.

Categories

Resources