When we use Border, the corner radius is not used for ClipToBound
see the solution with another border
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
public class ClippingBorder : Border { protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo) { OnApplyClip(); base.OnRenderSizeChanged(sizeInfo); } protected void OnApplyClip() { if (this.ClipToBounds) { RectangleGeometry _clipRect = new RectangleGeometry(); _clipRect.RadiusX = _clipRect.RadiusY = Math.Max(0.0, this.CornerRadius.TopLeft - (this.BorderThickness.Left * 0.5)); _clipRect.Rect = new Rect(0, 0, this.ActualWidth, this.ActualHeight); this.Clip = _clipRect; } } } |
For fill a Control you can use a canvas transformed to VisualBrush
If you change part of canvas you modify directly the render of VisualBrush
For this Canvas (set outside of window)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
<Canvas x:Name="back" Height="12" Width="12" HorizontalAlignment="Left" Margin="-45,21,0,0" VerticalAlignment="Top" Background="Black"> <Ellipse Height="6" Canvas.Left="0" Canvas.Top="0" Width="6"> <Ellipse.Stroke> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FF4C4C4C" Offset="0.01" /> <GradientStop Color="White" Offset="1" /> <GradientStop Color="White" Offset="0.538" /> <GradientStop Color="#FF4C4C4C" Offset="0.472" /> </LinearGradientBrush> </Ellipse.Stroke> </Ellipse> <Ellipse Height="6" Canvas.Left="6" Canvas.Top="6" Width="6"> <Ellipse.Stroke> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FF4C4C4C" Offset="0.01" /> <GradientStop Color="White" Offset="1" /> <GradientStop Color="White" Offset="0.538" /> <GradientStop Color="#FF4C4C4C" Offset="0.472" /> </LinearGradientBrush> </Ellipse.Stroke> </Ellipse> </Canvas> |
Create VisualBrush in Window.Resource
|
<Window.Resources> <VisualBrush x:Key="background" Visual="{Binding ElementName=back}" /> </Window.Resources> |
And using
|
<Rectangle Fill="{StaticResource background}" Margin="39,39,0,39"/> |
If you want to specify using of this VisualBrush
Stretch= »None » |
Define how the render is made |
TileMode= »Tile » |
Define how the render of each object is made |
AlignmentX= »Left » |
First aligment on X on render |
AlignmentY= »Top » |
First aligment on Y on render |
ViewportUnits= »Absolute » |
Type of viewport unit, if absolute no dependency of control, otherwise else |
Viewport= »0,0,12,12″ |
According of ViewportUnit definition |
Sample
|
<VisualBrush x:Key="background" Visual="{Binding ElementName=back}" Stretch="None" TileMode="Tile" AlignmentX="Left" AlignmentY="Top" Viewport="0,0,12,12" ViewportUnits="Absolute" /> |
Result
for overriding custom color like
|
<SolidColorBrush x:Key="xcolor" Color="#6D9DFF" /> |
redefine the same SolidColorBrush with the same Key
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
<!-- using resource --> <Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="43" Background="{StaticResource xcolor}" Margin="366,177,0,0" VerticalAlignment="Top" Width="116" /> <!-- redefine resource --> <Grid Margin="366,225,0,0" HorizontalAlignment="Left" Width="116" Height="43" VerticalAlignment="Top"> <Grid.Resources> <SolidColorBrush x:Key="xcolor" Color="red" /> </Grid.Resources> <Border BorderBrush="Black" BorderThickness="1" Background="{StaticResource xcolor}" /> </Grid> |
An extension methode for transforme all FrameworkElement to BitmapSource
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
public static BitmapSource ToBitmapSource(this FrameworkElement element) { double dw = Math.Max(element.ActualWidth, element.Width); double dh = Math.Max(element.ActualHeight, element.Height); // needed for no visible FrameworkElement if (!element.IsVisible) { var size = new Size(dw, dh); element.Measure(size); element.Arrange(new Rect(size)); } int width = (Int32)Math.Ceiling(dw); int height = (Int32)Math.Ceiling(dh); RenderTargetBitmap bmp = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32); DrawingVisual drawingVisual = new DrawingVisual(); using (DrawingContext drawingContext = drawingVisual.RenderOpen()) { VisualBrush visualBrush = new VisualBrush(element); drawingContext.DrawRectangle( visualBrush, null, new Rect(new Point(), new Size(width, height))); } bmp.Render(drawingVisual); return bmp; } |
Show all binding possibility i have seen
Simple binding |
Description |
{Binding}
{Binding .} |
Binding to DataContext |
{Binding Name}
{Binding Path=Name} |
Binding to « Name » property of object contained in DataContext |
{Binding Selected.Name}
{Binding Path=Selected.Name} |
Binding to Selected and inside selected to « Name » property of object contained in DataContext |
|
|
Binding to xaml object |
Description |
{Binding Width,ElementName=alpha}
{Binding Path=Width,ElementName=alpha} |
Binding to another control in xaml (x:Name must be specified) |
{Binding DataContext.IsDirty,ElementName=alpha} |
Binding to DataContext contained in Control named alpha and set IsDirty |
|
|
Binding to static |
Description |
{Binding Source={x:Static s:DateTime.Now}}
{x:Static s:DateTime.Now} |
Binding on Static DateTime with |
{Binding Source={StaticResource ElementKey}}
{StaticResource ElementKey} |
Binding on StaticResource named « ElementKey » |
{Binding Source={DynamicResource ElementKey}}
{DynamicResource ElementKey} |
Binding on DynamicResource named « ElementKey » |
{x:Static diisyetem:Node.Nodes} |
Binding on static Nodes |
|
|
Binding to Singleton |
Description |
{Binding ShowGrid, Source={x:Static local:DesignBackgroundDatas.Current }} |
Binding on current static local:DesignBackgroundDatas (singleton current) and binding to ShwoGrid |
|
|
binding self to compoment |
Description |
Width=“{Binding ActualHeight, RelativeSource={RelativeSource Self}}“ |
Binding to itself for setting Width from ActualHeight |
|
|
binding to template (for CustomControl) |
Description |
{TemplateBinding ToolPlay} |
Simple binding to property of CustomControl |
{Binding PlayCommand, RelativeSource={RelativeSource TemplatedParent}} |
Same as previous but can speciy if necessary Mode, Converter etc …
or when templatebinding don’t work (sometime possible) |
|
|
binding parent élément (hierarchical find) |
Description |
{Binding Foreground,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}} |
Binding on Foreground of parent Button (first find) |
{Binding Path=Tag,RelativeSource={RelativeSource Mode=FindAncestor,AncestorLevel=2,AncestorType=Grid} |
Binding on tag of second parent grid (on up from compoment first grid parent finded is ignored and second is set) |
|
|
Binding to attached property |
Description |
{Binding Path=(local:Attached.Test),
RelativeSource={RelativeSource Self}} |
Specified with () Binding to AttachedTest property of current control (self)
attached property are set only for Control (you must specify it) |
{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ContentControl},
Path=(AutomationProperties.AutomationId)} » |
Same as previous but with ancestor for binding to attached property AutomationProperties.AutomationId of contentcontrol |
Samples
<TextBlock x:Name=“t1“ local:TestClass.Test=“abc“ Grid.Column=“3“/>
<TextBlock Text=“{Binding Path=(local:TestClass.Test), ElementName=t1}“/>
<TextBlock Text=“{Binding (Grid.Column), ElementName=t1}“/> |
|
|
|
Binding to array/Collection (specific) |
Description |
{Binding Food[2]} |
Binding of element 2 of array Food |
Content=“{Binding Path=/}“ |
/ char set the binding of current item in collection (in datacontext) |
Content=“{Binding Path=/IsDirty}“ |
Same as previous with set of IsDirty property of current item in collection |
|
|
MultiBinding |
Description |
<MultiBinding StringFormat=“{}{0} x {1}“>
<Binding Path=“Width“ />
<Binding Path=“Height“ />
</MultiBinding> |
Binding for create a text without converter |
<MultiBinding Converter=“{StaticResource MultiValueConverter}“>
<Binding Path=“FirstName“/>
<Binding Path=“Surname“ />
</MultiBinding>
with
<local:NameMultiValueConverter x:Key=“NameMultiValueConverter“ /> |
Multibinding using IMultiValueConverter
public class NameMultiValueConverter : IMultiValueConverter
{
public object Convert(object[] values,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture) {
return String.Format(“{0} {1}“, values[0], values[1]);
}
} |
|
|
For synchronize SelectedItem with focused component in DataTemplate for exemple
|
<ListBox VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch"> <ListBox.ItemContainerStyle> <Style TargetType="{x:Type ListBoxItem}"> <Style.Triggers> <Trigger Property="IsKeyboardFocusWithin" Value="True"> <Setter Property="IsSelected" Value="True" /> </Trigger> </Style.Triggers> </Style> </ListBox.ItemContainerStyle> </ListBox> |
here is the common code for having DoEvents() function for WPF
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#region DoEvents private static DispatcherOperationCallback exitFrameCallback = new DispatcherOperationCallback(ExitFrame); /// <summary> /// Processes all UI messages currently in the message queue. /// </summary> public static void DoEvents() { // Create new nested message pump. DispatcherFrame nestedFrame = new DispatcherFrame(); // Dispatch a callback to the current message queue, when getting called, // this callback will end the nested message loop. // note that the priority of this callback should be lower than the that of UI event messages. DispatcherOperation exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke( DispatcherPriority.Background, exitFrameCallback, nestedFrame); // pump the nested message loop, the nested message loop will // immediately process the messages left inside the message queue. Dispatcher.PushFrame(nestedFrame); // If the "exitFrame" callback doesn't get finished, Abort it. if (exitOperation.Status != DispatcherOperationStatus.Completed) { exitOperation.Abort(); } } private static Object ExitFrame(Object state) { DispatcherFrame frame = state as DispatcherFrame; // Exit the nested message loop. frame.Continue = false; return null; } #endregion |
place this in App class and use App.DoEvents();
Commentaires récents