Created
February 14, 2020 00:59
-
-
Save CheetahChrome/e3cf1e09b29075547ea935afc82e7c9b to your computer and use it in GitHub Desktop.
ItemsControl which has the content control
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class People : List<Person> { } | |
public class Person | |
{ | |
public string First { get; set; } | |
public string Last { get; set; } | |
public string Phone { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ResizeThumb : Thumb | |
{ | |
public ResizeThumb() => DragDelta += new DragDeltaEventHandler(this.ResizeThumb_DragDelta); | |
private void ResizeThumb_DragDelta(object sender, DragDeltaEventArgs e) | |
{ | |
if (this.DataContext is Control designerItem) | |
{ | |
double deltaVertical, deltaHorizontal; | |
switch (VerticalAlignment) | |
{ | |
case VerticalAlignment.Bottom: | |
deltaVertical = Math.Min(-e.VerticalChange, designerItem.ActualHeight - designerItem.MinHeight); | |
designerItem.Height -= deltaVertical; | |
break; | |
case System.Windows.VerticalAlignment.Top: | |
deltaVertical = Math.Min(e.VerticalChange, designerItem.ActualHeight - designerItem.MinHeight); | |
Canvas.SetTop(designerItem, Canvas.GetTop(designerItem) + deltaVertical); | |
designerItem.Height -= deltaVertical; | |
break; | |
default: | |
break; | |
} | |
switch (HorizontalAlignment) | |
{ | |
case System.Windows.HorizontalAlignment.Left: | |
deltaHorizontal = Math.Min(e.HorizontalChange, designerItem.ActualWidth - designerItem.MinWidth); | |
Canvas.SetLeft(designerItem, Canvas.GetLeft(designerItem) + deltaHorizontal); | |
designerItem.Width -= deltaHorizontal; | |
break; | |
case System.Windows.HorizontalAlignment.Right: | |
deltaHorizontal = Math.Min(-e.HorizontalChange, designerItem.ActualWidth - designerItem.MinWidth); | |
designerItem.Width -= deltaHorizontal; | |
break; | |
default: | |
break; | |
} | |
} | |
e.Handled = true; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window.Resources> | |
<local:People x:Key="People"> | |
<local:Person First="Joe" Last="Smith" Phone="303-555-5555"/> | |
<local:Person First="Jenny" Last="Johnson" Phone="720-867-5309" /> | |
<local:Person First="Frank" Last="Wright" Phone="202-555-5555" /> | |
</local:People> | |
<Style TargetType="{x:Type ContentControl}"> | |
<Setter Property="Template"> | |
<Setter.Value> | |
<ControlTemplate TargetType="{x:Type ContentControl}"> | |
<Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"> | |
<Control x:Name="resizer"> | |
<Control.Style> | |
<Style TargetType="{x:Type Control}"> | |
<Setter Property="Template"> | |
<Setter.Value> | |
<ControlTemplate TargetType="{x:Type Control}"> | |
<Grid Margin="-3"> | |
<local:ResizeThumb Width="7" Height="7" Margin="-2" Cursor="SizeNWSE" VerticalAlignment="Bottom" HorizontalAlignment="Right"/> | |
</Grid> | |
</ControlTemplate> | |
</Setter.Value> | |
</Setter> | |
</Style> | |
</Control.Style> | |
</Control> | |
<ContentPresenter Content="{TemplateBinding Content}"/> | |
</Grid> | |
</ControlTemplate> | |
</Setter.Value> | |
</Setter> | |
</Style> | |
</Window.Resources> | |
<ItemsControl Margin="10" | |
ItemsSource="{Binding Source={StaticResource People}}"> | |
<ItemsControl.ItemsPanel> | |
<ItemsPanelTemplate> | |
<UniformGrid Rows="1" /> | |
</ItemsPanelTemplate> | |
</ItemsControl.ItemsPanel> | |
<ItemsControl.ItemTemplate> | |
<DataTemplate> | |
<Canvas> | |
<ContentControl Width="100" Height="100" > | |
<Grid> | |
<Ellipse Fill="Silver"> | |
<Ellipse.Effect> | |
<DropShadowEffect Color="Black" Direction="320" ShadowDepth="6" Opacity="0.5"/> | |
</Ellipse.Effect> | |
</Ellipse> | |
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> | |
<TextBlock Margin="3,3,3,0" Text="{Binding Path=First}"/> | |
<TextBlock Margin="3,0,3,7" Text="{Binding Path=Last}"/> | |
</StackPanel> | |
</Grid> | |
</ContentControl> | |
</Canvas> | |
</DataTemplate> | |
</ItemsControl.ItemTemplate> | |
<ItemsControl.ItemContainerStyle> | |
<Style TargetType="{x:Type ContentPresenter}"> | |
<Style.Triggers> | |
<Trigger Property="IsMouseOver" Value="True"> | |
<Setter Property="Grid.ZIndex" Value="1"/> | |
</Trigger> | |
</Style.Triggers> | |
</Style> | |
</ItemsControl.ItemContainerStyle> | |
</ItemsControl> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To reproduce create a .Net Core WPF app. Place the
window.xaml
code into the main page. Then create separate files forpeople.cs
and ResizeThumb.cs and provide the proper namespacing in the main windows xaml.