Xaml how to make a condition - sql

Hi Guys i have a problems
Well, I have a sql command that extracts the number of days remaining until the end of the order. Below is the code in XAML and the problem is that there is no idea how to do that e.g. if the number of days is greater than 5 then color green etc? It's about making a condition. I am a beginner and I am asking for some thought. Regards.
XAML CODE
<DataTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:user="clr-namespace:Okna.Documents.Views.User;assembly=Okna.Documents">
<Grid
x:Name="grid">
<TextBlock
Margin="3,2"
Text="{Binding RowData.Row.ud_pozostalo_dni2}" />
</Grid>
<DataTemplate.Triggers>
<DataTrigger
Binding="{Binding RowData.Row.ud_pozostalo_dni2}"
Value="8">
<Setter
TargetName="grid"
Property="Background"
Value="Green" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
SQL select datediff(day,getdate(), o.realizacja)

You can add a value converter for this.
An example converter can be:
public class CellColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
int val = (int)value;
if (val > 5)
{
return Brushes.Green;
}
return Brushes.Transparent;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
And in your XAML:
<Window.Resources>
<local:CellColorConverter x:Key="CellColorConverter"/>
</Window.Resources>
<Grid x:Name="grid">
<TextBlock
Margin="3,2"
Text="{Binding RowData.Row.ud_pozostalo_dni2}" Background="{Binding RowData.Row.ud_pozostalo_dni2,Converter={StaticResource CellColorConverter}}" />
</Grid>

Related

Tooltip position in Oxyplot

I am recently using OxyPlot to draw charts for my WPF project. I have an issue with the tooltip in a line series. The rectangle box of the tooltip is too close to the point my mouse locates. I am wondering if there is any way to modify the position of the tooltip box and make it further from the cursor. Thank a lot. For example, I would like to move the tooltip box (in the red box) to right a bit.
[Edit]
Following Anu's idea, I added the following code but seems like it not works as expected.
In the xaml.cs file, I added
<UserControl.Resources>
<popups:ScreenPointToOffSetScreenPointConverter x:Key="ScreenPointToOffSetScreenPointConverter"/>
</UserControl.Resources>
And then change my OxyPlot to
<ItemsControl ItemsSource="{Binding AmplitudeDtos}" Margin="0" Name="Amplitude" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid popups:GridUtilities.ItemsPerRow="1" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type dtoModels:SignalDto}">
<oxy:PlotView Height="Auto" Width="Auto" MinHeight="40" MinWidth="100" HorizontalContentAlignment="Stretch"
FontStyle="Normal" FontFamily="Roboto, Microsoft YaHei" FontSize="8" Model="{Binding PlotModel}" Controller="{Binding PlotController}" Padding="8" Margin="0">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding SignalLoadedCommand}" CommandParameter="{Binding}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<oxy:PlotView.DefaultTrackerTemplate>
<ControlTemplate>
<oxy:TrackerControl Position="{Binding Position, Converter={StaticResource ScreenPointToOffSetScreenPointConverter}}" LineExtents="{Binding PlotModel.PlotArea}">
<oxy:TrackerControl.Content>
<TextBlock Text="{Binding}" />
</oxy:TrackerControl.Content>
</oxy:TrackerControl>
</ControlTemplate>
</oxy:PlotView.DefaultTrackerTemplate>
</oxy:PlotView>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Please note that my OxyPlot is in a DataTemplate.
ScreenPointToOffSetScreenPointConverter class is the exactly the same as in Anu's example.
A relatively simple fix for the requirement would be to modify DefaultTrackerTemplate and use a Custom Converter to change the Position.For example, You could define a ScreenPoint converter as following
public class ScreenPointToOffSetScreenPointConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value is ScreenPoint screenPoint)
{
return new ScreenPoint(screenPoint.X + 50, screenPoint.Y);
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
// TODO: Implement
throw new NotImplementedException();
}
}
And then in your XAML
<oxy:PlotView Model="{Binding MyModel}">
<oxy:PlotView.DefaultTrackerTemplate>
<ControlTemplate>
<oxy:TrackerControl Position="{Binding Position,Converter={StaticResource ScreenPointToOffSetScreenPointConverter}}" LineExtents="{Binding PlotModel.PlotArea}">
<TextBlock Text="{Binding}" />
</oxy:TrackerControl>
</ControlTemplate>
</oxy:PlotView.DefaultTrackerTemplate>
</oxy:PlotView>
The Converter would add an Offset to the ScreenPoint position for the Tracker. You could modify the Offset Value to the Converter if it makes sense for your application requirement.

Set xaml object height as % of other xaml object

In a DataTemplate in my UWP app, I want to set the hight of a rectangle as a % of the hight of a image.
My code looks like this.
<DataTemplate
x:Key="FlipViewTemplate">
<Grid>
<Image
x:Name="image"
Margin="20"
Source="{Binding fullImgUrl}"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
<Rectangle
Height="{Binding
Converter={StaticResource PercentageConverter},
ElementName=image,
Path=ActualHeight,
ConverterParameter=0.2}"
Fill="#FFEA1E1E"
VerticalAlignment="Bottom" />
</Grid>
</DataTemplate>
And my converter like this
public class PercentageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return System.Convert.ToDouble(value) *
System.Convert.ToDouble(parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
My problem is that the value in the converter that should be the actualhight of the image is always 0.
You are already using a Grid, so you could simply define two rows. In the example below the Image control span both rows, i.e. the whole Grid, while the Rectangle resides in the second row only, which has 20% of the whole height.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="4*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Grid.RowSpan="2" .../>
<Rectangle Grid.Row="1" .../>
</Grid>

ListBox and ApplicationBar: last element padding

I have a ListBox and ApplicationBar with Opacity=.5. I want that ListBox items are under the ApplicationBar.
Like figure:
But when I scrolled to end of list then last element of ListBox is under the ApplicationBar.
I want that last element is over ApplicationBar.
Can I add padding for a last element of the ListBox (LongListSelector)? How can solved this issue?
UPDATE
<ListBox Name="MyListBox">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<i:Interaction.Triggers>
<ec:DataTrigger Binding="{Binding RelativeSource=
{RelativeSource Self},
Converter={StaticResource IsLastItemInContainerConverter}}"
Value="True">
<ec:ChangePropertyAction PropertyName="Padding" Value="0,0,0,72"/>
</ec:DataTrigger>
</i:Interaction.Triggers>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Background="Red"
Width="400"
Height="120"
Margin="15">
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
UPDATE 2
My problem can be easily solved by using LongListSelector. Just add empty ListFooterTemplate with Height = ApplicationBar height.
You can use converter to check if it's last item in listbox and in case yes, set padding to desired value via DataTrigger.
Check my answer over here for converter code. Just for sake of completeness of this answer i will post that converter code here:
public class IsLastItemInContainerConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
DependencyObject item = (DependencyObject)value;
ItemsControl ic = ItemsControl.ItemsControlFromItemContainer(item);
return ic.ItemContainerGenerator.IndexFromContainer(item)
== ic.Items.Count - 1;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
and use it like this:
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource=
{RelativeSource Self},
Converter={StaticResource IsLastItemInContainerConverter}}"
Value="True">
<Setter Property="Padding" Value="5"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
edit: Didn't see the update to the original question. The below remains for additional detail.
This solution is for Windows Phone 8 only (although you could use the LongListSelector from the Windows Phone Toolkit if you are targeting 7.1/5).
I'd replace the ListBox with the LongListSelector from the SDK
<Grid>
<controls:LongListSelector ItemTemplate="{StaticResource Item}" ListFooterTemplate="{StaticResource Footer}" />
</Grid>
and for the templates add
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="Item">
<StackPanel Background="Red"
Width="400"
Height="120"
Margin="15">
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="Footer">
<Border Height="72" />
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
Maybe simplest solution is to customize ItemsPanel property on ListBox:
<ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Margin="0,0,0,150"/> <!-- set margin to show some 'blank' space after last item -->
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
...
</ListBox>
But be aware that by default ListBox uses VirtualizedStackPanel as ItemsPanel. If you change it to StackPanel, it may bring some performance penalty. This is solution is suitable for lists with few items.

xaml code not working - c#

I have following code:
<DataTemplate DataType="{x:Type abc}">
<StackPanel Orientation="Horizontal">
<CheckBox Margin="0,0,3,0" x:Name="CheckBox">
<CheckBox.IsChecked>
<Binding Path="IsSelected"
Mode="TwoWay">
<Binding.RelativeSource>
<RelativeSource Mode="Template" />
</Binding.RelativeSource>
</Binding>
</CheckBox.IsChecked>
</CheckBox>
<TextBlock Text="{Binding}"
Margin="0,0,10,5" />
<TextBlock Text="{Binding Channel}"
Margin="7,0,0,0"
Visibility="{Binding Path=IsChannelVisible,ElementName=View, Converter={StaticResource BooleanToVisibile}}" />
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Valid}" Value="true">
<Setter TargetName="CheckBox" Property="Foreground" Value="#000000"/>
</DataTrigger>
<DataTrigger Binding="{Binding Valid}" Value="false">
<Setter TargetName="CheckBox" Property="Foreground" Value="#999999"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
What i am trying to achieve is change the color of the the checkbox text. But above code is not working. Can anyone please help me with this?
Tried code from Tilak:
<ControlTemplate x:Key="Template"
TargetType="ListViewItem">
<StackPanel Orientation="Horizontal">
<CheckBox Margin="7,0,0,0" Foreground={Binding Valid, Converter={StaticResource ValidToColorConverter}}
x:Name="CheckBox">
<CheckBox.IsChecked>
<Binding Path="IsSelected"
Mode="TwoWay">
<Binding.RelativeSource>
<RelativeSource Mode="Parent" />
</Binding.RelativeSource>
</Binding>
</CheckBox.IsChecked>
</CheckBox>
<ContentPresenter />
<DataTemplate.CheckBox>
<Intellivu:ValidToColorConverter x:Key="ValidToColorConverter" />
</DataTemplate.CheckBox>
</StackPanel>
</ControlTemplate>
This is not working? Am i doing something wrong?
You can use converter and bind to Foreground property directly.
Following following steps for it
Create ValidToColorConverter
public class ValidToColorConverter : IValueConverter
{
// This converts the DateTime object to the string to display.
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
bool valid = value as bool;
return valid ? Colors.Black : new SolidColorBrush(Colors.FromArgb(255,99,99,99));
}
// No need to implement converting back on a one-way binding
public object ConvertBack(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Add ValidToColorConverter as static resource in the resources corresponding to control
<DataTemplate.Resources>
<local:ValidToColorConverter x:Key="ValidToColorConverter" />
</DataTemplate.Resources>
Note - local should be added into page namespaces(corresponding to namespace where converter is defined)
Refer to Converter in the Checkbox
<CheckBox Margin="0,0,3,0" Foreground={Binding Valid, Converter={StaticResource ValidToColorConverter}} x:Name="CheckBox">

Listbox Item Template for an empty list

I have a list box with an item template defined in XAML like this:
<ListBox Name="listBoxDisruptions">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,10">
<TextBlock Text="{Binding text}" Foreground="Black" FontSize="29">Hello! some item</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Now what i want is to display a line of text in the center of the listbox in case the ItemSource for this listbox is empty.
Does XAML support some kind of no item template, ? something like this:
<ListBox Name="listBoxDisruptions">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,10">
<TextBlock Text="{Binding text}" Foreground="Black" FontSize="29">Hello! some item</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.NoItemTemplate>
<TextBlock Text="No Items to display"/>
</ListBox.NoItemTemplate>
</ListBox>
So ?
There might be a XAML way to do it using WPF-like techniques - Listbox Item Template for an empty list
However, in Overflow7 I got bored trying to make these work - so I used a slightly-hacky trick instead of adding an extra TextBlock to the page and then using:
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) {
listBox1.ItemsSource = data;
data.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(data_CollectionChanged);
}
void data_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
if (data.Count == 0)
textBlock1.Visibility = Visibility.Visible;
else
textBlock1.Visibility = Visibility.Collapsed;
}
(Trick learnt from http://forums.create.msdn.com/forums/p/70755/431687.aspx)
You could take the textblock outside of the listview, and then bind the visibility of the textblock to the count of the list your using for the listview, using a converter.
EDIT: Example as asked for:-
<ListView ItemsSource="{Binding MyItemSource}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<TextBlock Text="Some text" Visibility="{Binding MyItemSource.Count, Converter={StaticResource CountToVisibilityConverter}}"/>
Declare the converter either in your page resources, or a resource dictionary in your app like so:
<converters:CountToVisibilityConverter x:Key="CountToVisibilityConverter" />
and then the converter could be:
public sealed class CountToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string culture)
{
if (value != null)
{
var i = (Int32)value;
if (i > 0)
return Visibility.Collapsed;
else
return Visibility.Visible;
}
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, string culture)
{
return new NotImplementedException();
}
}