Adding Border for GridViewCell Programmatically - c#

I'm trying to add a Thick Border around a certain column programmatically. I can change the Background property using identical methods but the border property won't change in anyway.
if (eligibleProperties.Contains(column.Header.ToString()))
{
Setter s1 = new Setter(GridViewCell.BorderThicknessProperty, new Thickness(7));
Setter s2 = new Setter(BorderBrushProperty, Brushes.Black);
Style st = new Style(typeof(GridViewCell));
st.Setters.Add(s1);
st.Setters.Add(s2);
column.CellStyle = st;
}
Working code for Background change
if (eligibleProperties.Contains(column.Header.ToString()))
{
Setter s2 = new Setter(BackgroundProperty, Brushes.Black);
Style st = new Style(typeof(GridViewCell));
st.Setters.Add(s2);
column.CellStyle = st;
}

Because of the default template structure of a Telerik GridViewCell, modifying the cell borders is actually more complicated than just setting the BorderThickness and BorderBrush properties.
You will need to modify the control template. Please refer to the following threads for more information about this.
How do I change border color in cell style selector: https://www.telerik.com/forums/how-do-i-change-border-color-in-cell-style-selector
Change the row border colour: https://www.telerik.com/forums/change-the-row-border-colour

I believe your best bet is to change the template of the cells for that column, you can even do it on the fly by setting the telerik:GridViewDataColumn.CellStyle of the column to this:
<Style TargetType="{x:Type telerik:GridViewCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="telerik:GridViewCell">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
<ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" ToolTip="{TemplateBinding ToolTip}" VerticalAlignment="Center" Margin="3,0"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Now your cells have the properties BorderThicknessand BorderBrush to work with. Like in this example below your cells will have a thick red border all around:
<telerik:GridViewDataColumn Header="MyColumn1" DataMemberBinding="{Binding MyColumn1Binding}" Width="150" >
<telerik:GridViewDataColumn.CellStyle>
<Style TargetType="{x:Type telerik:GridViewCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="telerik:GridViewCell">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
<ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" ToolTip="{TemplateBinding ToolTip}" VerticalAlignment="Center" Margin="3,0"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Propert="BorderBrush" Value="Red" />
<Setter Propert="BorderThickness" Value="2,2,2,2" />
</Style>
</telerik:GridViewDataColumn.CellStyle>
</telerik:GridViewDataColumn
Using the thickness you could for example also hide the top and bottom borders. In the style you can also make use of Style.Triggers and define different border settings based on multiple DataTriggers.
I believe this offers much more flexibility than adding the borders programmatically. However, if you really want to do this in code then you can still define the template in XAML as in my first example which should make the setters from your code have affect.
And if this still doesn't seem sufficient enough, defining the template of the cell completely programmatically would be your next best bet. If you need help to translate the XAML to actual code just leave a comment, although I believe doing it in XAML is a lot better.

Related

Is it possible to apply MahApps’s TextBoxHelper to a ScrollViewer?

I’m building a WPF application in which there’s a TextBox. To apply rounded corners to it, here I read that I need to create a specific style using a ScrollViewer inside a Border. So far so good, but I want to apply the funcionalities of MahApps’ TextBoxHelper class, which lets you use stuff like:
<TextBox controls:TextboxHelper.Watermark="I’m a watermark"/>
to display a watermark inside the TextBox. The problem is that I don’t understand how to combine the style (using the advices in the article I linked to) with MahApps. I tried something like:
<Style TargetType="TextBox" x:Key="myKey">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border x:Name="border" CornerRadius="10">
<ScrollViewer x:Name="PART_ContentHost"
Focusable="false"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Hidden"
ctrl:TextboxHelper.Watermark="true"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
but the watermark doesn’t show.

How do you bind a specific Color of a specific column within an OxyPlot LinearBarSeries in XAML in a c# WPF project?

Any help here is appreciated:
I would like to associate through binding in XAML (or some other method as long as it works) a specific bar column color to data in my bar series. Ultimately, I would like to have one color per column item. I suspect color attribute could be added to the class where the column item data value is stored. So far, by default, all I get is the same color for every bar column in my graph...very boring to say the least - you'd think all bar series plotting utilities can change the color of any one column in a bar series right?! If OxyPlot can do it - I haven't found the solution yet.
I tried to create a custom Style to see if I could gain some insight into the LinearBarSeries class, but it was vague. I then looked inside these classes: DataPointSeries, DataPoint, ColumnItem,ColumnSeries, and BarItemBase, but I'm more confused than ever.
My XAML so looks like this and basically it comes from the example provided by OxyPlot's LinearBarSeries example:
<Window>
<Window.Resources>
<Style x:Key="LinearBarSeriesStyle1" TargetType="{x:Type oxy:LinearBarSeries}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type oxy:LinearBarSeries}">
<Border Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<DockPanel>
<oxy:Plot Title="LinearBarSeries">
<oxy:Plot.Annotations>
<oxy:LineAnnotation Type="Horizontal" Y="0"></oxy:LineAnnotation>
</oxy:Plot.Annotations>
<oxy:Plot.Axes>
<oxy:DateTimeAxis IntervalType="Hours" IntervalLength="50"/>
</oxy:Plot.Axes>
<oxy:LinearBarSeries Style="{DynamicResource LinearBarSeriesStyle1}" ItemsSource="{Binding Pnls}" Title="P&L" DataFieldX="Time" DataFieldY="Value" FillColor="#454CAF50" StrokeColor="#4CAF50" StrokeThickness="1" BarWidth="5">
</oxy:LinearBarSeries>
</oxy:Plot>
</DockPanel>
</Window>
So far I have no method to change column item color for any data column. Please help if you can, Thanks!
I found it easier to use a PlotModel for OxyPlot customization as in
<oxyWpf:Plot Model="{Binding PlotModel}" />
Thus you can add multiple Series with different colours and add your data to it:
var blueLineSeries = new LineSeries{Color = OxyColors.Blue};
blueLineSeries.Points.Add(new DataPoint(/*...*/));
PlotModel.Series.Add(blueLineSeries);
PlotModel.Series.Add(new LineSeries{Color = OxyColors.Green});
There should be plenty of samples at OxyPlot's GitHub repository or other resources.

WPF Text character 'g', 'q' and others get cut off in a TabItem

I have a TabControl with a custom template. The TabItems also have a custom template:
<ControlTemplate TargetType="{x:Type TabItem}">
<Border Background="{TemplateBinding Background}" Padding="10,5,10,5">
<ContentPresenter ContentSource="Header" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Border>
</ControlTemplate>
Something basic but still. The issue is that if the header contains characters that are a tad longer (like g, q or p), their bottom side gets cropped off:
Is there a workaround for this? I have tried adding a margin to the content presenter instead of the padding and the result is the same.
Change the paddling, try to put 0 from bottom and also change paddling of control to 0 where this ControlTemplate is going to be used.
In your case:
<Border Background="{TemplateBinding Background}" Padding="10,5,10,0">
<ContentPresenter ContentSource="Header" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Border>

Sending a parameter from user control that uses a custom control

I want to pass a parameter from a User Control that uses a Custom Control, and use that on my Custom Control's cs. For example, if I had a Custom Control on a UserControl
In UserControl (e.g. ThisViewName.XAML):
<ctrl:PinWindowControl Tag="ThisViewName" Grid.Row="0"/>
Which pretty much just contains a button
Generic.xaml:
<Style TargetType="{x:Type local:PinWindowControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:PinWindowControl}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Button Width="100" Height="100"></Button>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
How can I get the Tag all the way to my PinWindowControl.cs file?
Assuming you created a DependencyProperty in your PinWindowControl:
You can access Tag using this.Tag. Your PinWindowControl is a partial class that is associated with your XAML.
public class PinWindowControl.cs : FrameworkElement
{
public PinWindowControl()
{
Debug.WriteLine(this.Tag);
}
}

WPF 4.0 smooth-scrolling and UI visualization on custom control

My custom control should contains 1000000+ items. I've implemented the custom control based on ItemsControl. The source XAML of the control is:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:behaviors="clr-namespace:MyTestApplication.Behaviors"
xmlns:helpers="clr-namespace:MyTestApplication.Helpers"
xmlns:local="clr-namespace:MyTestApplication.Controls"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
<Style TargetType="{x:Type local:RowsWrapperControl}">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Recycling" />
<Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:RowsWrapperControl}">
<ScrollViewer x:Name="PART_ItemsScrollViewer"
Margin="{TemplateBinding Margin}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CanContentScroll="True"
HorizontalScrollBarVisibility="Disabled"
Padding="{TemplateBinding Padding}"
VerticalScrollBarVisibility="Hidden">
<i:Interaction.Behaviors>
<behaviors:ScrollViewerOffsetBehavior VerticalOffset="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=VerticalScrollOffset, Mode=OneWay}" />
</i:Interaction.Behaviors>
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
The virtualizing works good, but scrolling works only item-by-item, not pixel-by-pixel. I tried to set the CanContentScroll property to False , but it breaks the visualization. I found some helpful
information here and here, but it takes no effect for me.
Any idea how to turn on the smooth scrolling?
I have .Net 4.5 installed on my PC, but the target platform is .Net 4.0.
UPDATE:
The solution was found here.
do you have a simple list of data or tree alike data structure? What does your custom control do or should do? Only TreeView in WPF .NET 4.0 supports scrolling by pixels. A ListBox scrolls by items/units. Derivate your custom control from TreeView and you should do fine. Or wrap it in a TreeView. TreeView may also display simple list of data without hierarchical structure. I actually ment many people telling me they place their lists in TreeView just to have that pixel scrolling effect because it looks better than jumping over items/units/data however you call it.
Try it out :) I could place this as comment since its too large but if it works mark it as answer.

Categories