wpf usercontrol resizing with thumb not firing DragDelta - wpf

I have created a Usercontrol that I would like to make resizable using the thumb control. I have added my control to a Canvas and when I click on the thumb control the only events that are fired are Dragend and DragStart (in that order) and DragDelta is not even firing. I have found someone with a similar problem Thumb inside an ItemsControl not firing DragDelta event but there is no way I can ask them if they found a solution.
This is the code :
<UserControl x:Class="test.ucChair"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="53" d:DesignWidth="93" RenderTransformOrigin="0.5,0.5" Background="Beige"
>
<Grid Name="grd" Margin="0,0,0,0">
<Rectangle x:Name="rectAngle" Margin="0,0,0,0" Stroke="Black" RenderTransformOrigin="0.5,0.5" Grid.ZIndex="1000" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MinHeight="53" /> <!-- Width="93" Height="53"-->
<TextBox x:Name="txtName" Text="OPEN" Margin="0,0,0,0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" IsReadOnly="True" IsReadOnlyCaretVisible="False" TextWrapping="Wrap" Width="93" KeyDown="txtName_KeyDown" FlowDirection="RightToLeft" BorderThickness="0" FontSize="14" Cursor="Hand" TextAlignment="Center"/>
<Thumb Name="thmbResize" DockPanel.Dock="Right" VerticalAlignment="Bottom" Height="15" Width="15"
DragDelta="OnResizeThumbDragDelta"
DragStarted="OnResizeThumbDragStarted"
DragCompleted="OnResizeThumbDragCompleted" HorizontalAlignment="Right">
</Thumb>
</Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace test
{
/// <summary>
/// Interaction logic for ucChairLeft.xaml
/// </summary>
public partial class ucChair : UserControl
{
public ChairType AngleType;
//?? public int SectionID;
public ucChair()//ChairType CType)//?, int _SectionID)
{
InitializeComponent();
}
private void OnResizeThumbDragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
System.Diagnostics.Debug.WriteLine("DragEnd");
Mouse.Capture(null);
}
private void OnResizeThumbDragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
System.Diagnostics.Debug.WriteLine("DragDelta");
}
private void OnResizeThumbDragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
Mouse.Capture(this);
System.Diagnostics.Debug.WriteLine("DragStart");
}
}
}
<Window xmlns:test="clr-namespace:test" x:Class="test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="300">
<Canvas Width="300" Height="300" Background="LightBlue">
<test:ucChair Canvas.Left="50" Canvas.Top="100" Width="50" Height="50" Background="Yellow"/>
</Canvas>
</Window>
Any help greatly appreciated!

Related

WPF tile search in list view

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:EPOS.Desktop.UserControls"
xmlns:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol"
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls" xmlns:dxnav="http://schemas.devexpress.com/winfx/2008/xaml/navigation"
x:Class="EPOS.Desktop.UserControls.QeueOrders" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="600"
xmlns:tiles="clr-namespace:EPOS.Desktop.ViewModel">
<UserControl.DataContext>
<tiles:DemoViewModel x:Name="xyz"/>
</UserControl.DataContext>
<UserControl.Resources>
<StackPanel x:Key="tileTemplate">
<ListView>
<ListView.ItemTemplate>
<dxlc:Tile Content="{Binding Name}"/>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</UserControl.Resources>
<Grid>
<dxlc:TileLayoutControl ItemsSource="{Binding demos}" ItemTemplate="{StaticResource tileTemplate}" Grid.RowSpan="2"
Margin="21,0,-21,0"/> <!-- <dxlc:Tile Header="{Binding Name}" Click="Tile_Click" /> -->
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="144,10,0,0" TextWrapping="Wrap" Text="TextBox"
VerticalAlignment="Top" Width="120"/>
</Grid>
</UserControl>
I want search tiles in WPF. So far I have found that it's possible only if I use listview. If you know any other best possible way, please share the solution.
this is xml:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:EPOS.Desktop.UserControls"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
xmlns:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol" xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls" xmlns:dxnav="http://schemas.devexpress.com/winfx/2008/xaml/navigation" x:Class="EPOS.Desktop.UserControls.QeueOrders"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="600"
xmlns:tiles="clr-namespace:EPOS.Desktop.ViewModel"
>
<UserControl.DataContext>
<tiles:DemoViewModel x:Name="xyz"/>
</UserControl.DataContext>
<Grid>
<!--<dxlc:TileLayoutControl ItemsSource="{Binding demos}" ItemTemplate="{StaticResource tileTemplate}" Grid.RowSpan="2" Margin="21,0,-21,0" />-->
<!-- <dxlc:Tile Header="{Binding Name}" Click="Tile_Click" /> -->
<dxlc:TileLayoutControl Name="tileLayoutControl" ItemsSource="{Binding demos}" >
<dxlc:TileLayoutControl.ItemTemplate>
<DataTemplate>
<dxlc:Tile Header="{Binding Name}" Content="{Binding Name}"/>
</DataTemplate>
</dxlc:TileLayoutControl.ItemTemplate>
</dxlc:TileLayoutControl>
<Grid>
<dxe:SearchControl Name="search" Height="30" Width="200" Margin="38,0,362,270"/>
</Grid>
</Grid>
</UserControl>
This is with code-behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using EPOS.Desktop.ViewModel;
using EPOS.Desktop.View;
using EPOS.Desktop.Model;
using System.ComponentModel;
using DevExpress.Xpf.Editors;
using DevExpress.Xpf.LayoutControl;
namespace EPOS.Desktop.UserControls
{
/// <summary>
/// Interaction logic for QeueOrders.xaml
/// </summary>
public partial class QeueOrders : UserControl
{
public QeueOrders()
{
InitializeComponent();
DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(SearchControl.SearchTextProperty, typeof(SearchControl));
if (dpd != null)
{
dpd.AddValueChanged(search, OnSearchTextChanged);
}
}
private void OnSearchTextChanged(object sender, EventArgs e)
{
SearchControl tb = sender as SearchControl;
foreach (Tile t in tileLayoutControl.GetLogicalChildren(false))
{
if ((String.IsNullOrEmpty(tb.SearchText)) || (t.Content != null && t.Content.ToString().Contains(tb.SearchText)))
t.Visibility = Visibility.Visible;
else
t.Visibility = Visibility.Collapsed;
}
}
DemoViewModel demovm = new DemoViewModel();
private void Tile_Click(object sender, EventArgs e)
{
CategoryForm catform = new CategoryForm();
catform.ShowDialog();
}
}
}
But i want to do using MVVM patttern.

Remove an attribute from xaml using c# - WPF

This question is related to removing an attribute in xaml.
In the below code, I have a Span. During an event, I add a back ground to this Span. During another event, I need to remove it. Please let me know, if there is a way to remove the background attribute I set to Span.
My Xaml looks like this.
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication4"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
</Grid.RowDefinitions>
<RichTextBox x:Name="rtb">
<FlowDocument>
<Paragraph>
<Span x:Name="def" Tag="default">
<Run x:Name="deg">Some Text</Run>
</Span>
</Paragraph>
</FlowDocument>
</RichTextBox>
<TextBox x:Name="tx" Grid.Row="1" TextWrapping="Wrap"/>
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Content="s" Click="Button_Click"/>
<Button Content="bg-Add" Grid.Column="1" Click="Button_Click"/>
<Button Content="bg-Remove" Grid.Column="2" Click="Button_Click"/>
</Grid>
</Grid>
</Window>
My code looks like this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows. Shapes;
namespace WpfApplication4
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if ((sender as Button).Content.ToString() == "s")
{
tx.Text = XamlWriter.Save(def);
}
else if ((sender as Button).Content.ToString() == "bg-Add")
{
def.Background = new SolidColorBrush(Colors.Blue);
}
else if ((sender as Button).Content.ToString() == "bg-Remove")
{
//Need to remove the set back color so that I get the default back
}
}
}
}
I am able to find an answer myself.
def.ClearValue(Span.BackgroundProperty);
is going to remove that attribute for me.
You can override ShouldSerializeProperty
public class CustomizedSpan : Span
{
public bool IsRemoveBackGround;
protected override bool ShouldSerializeProperty(DependencyProperty dp)
{
if (dp == Span.BackgroundProperty && IsRemoveBackGround)
{
return false;
}
else
{
return base.ShouldSerializeProperty(dp);
}
}
}

How to Enable/Disable button in wpf

I Have 2 button , Start And capture. I want disable capture button on form load and enable start. and on after click on start disable start button and enable capture.
Please help me.
Thanks in advance
Edit
<Grid>
<Button Content="Button" Height="77" HorizontalAlignment="Left" Margin="92,151,0,0" Name="button1" VerticalAlignment="Top" Width="109">
<Button.Background>
<ImageBrush ImageSource="/WpfApplication1;component/Images/images.jpg" />
</Button.Background>
</Button>
<Button Content="Button" Height="77" HorizontalAlignment="Left" Margin="229,151,0,0" Name="button2" VerticalAlignment="Top" Width="112" IsEnabled="False" >
<Button.Background>
<ImageBrush ImageSource="/WpfApplication1;component/Images/images.jpg" />
</Button.Background>
</Button>
</Grid>
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Name="BStart" Content="Start" Width="100" Height="50" HorizontalAlignment="Left" Click="BStart_Click" />
<Button Name="BCapture" Content="Capture" Width="100" Height="50" HorizontalAlignment="Right" IsEnabled="False" />
</Grid>
</Window>
you can disable Capture by xaml and then enable it by writing code in c#.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void BStart_Click(object sender, RoutedEventArgs e)
{
BStart.IsEnabled = false;
BCapture.IsEnabled = true;
}
}
}
for enable and disable use IsEnabled Property. and for clicking on the button use Click event.
Set IsEnabled="False" to disable the button
<Button Name="btn" IsEnabled="False" Content="press"/>
Set IsEnabled="True" to enable the button
<Button Name="btn" IsEnabled="True" Content="press"/>
Hope this helps
You should store current state in some variable (e.g. _capturing). When variable changing, you refresh IsEnabled property.
Xaml code:
<Button Content="Start" Name="StartButton" Grid.Row="0" Click="StartButton_Click" />
<Button Content="Capture" Name="CaptureButton" Grid.Row="1" Click="CaptureButton_Click"/>
C# code:
public partial class MainWindow : Window
{
private bool _capturing;
public MainWindow()
{
InitializeComponent();
// Some code
_capturing = false;
UpdateButtons();
}
private void StartButton_Click(object sender, RoutedEventArgs e)
{
// Some code
_capturing = true;
UpdateButtons();
}
private void CaptureButton_Click(object sender, RoutedEventArgs e)
{
// Some code
UpdateButtons();
}
private void UpdateButtons()
{
StartButton.IsEnabled = !_capturing;
CaptureButton.IsEnabled = _capturing;
}
}
UPDATE
You should add click handler and your xaml code will work:
<Button Click="StartButton_Click" Content="Button" Height="77" HorizontalAlignment="Left" Margin="92,151,0,0" Name="button1" VerticalAlignment="Top" Width="109">
<Button.Background>
<ImageBrush ImageSource="/WpfApplication1;component/Images/images.jpg" />
</Button.Background>
</Button>
<Button Click="CaptureButton_Click" Content="Button" Height="77" HorizontalAlignment="Left" Margin="229,151,0,0" Name="button2" VerticalAlignment="Top" Width="112" IsEnabled="False" >
<Button.Background>
<ImageBrush ImageSource="/WpfApplication1;component/Images/images.jpg" />
</Button.Background>
</Button>
Yes I got my answer. Just make simeple solution
btnhide.visibility= system.visibility. hidden;
There are two ways to disable a button In C# WPF.
In .xaml File
Using IsEnabled="False". If you set False to IsEnabled attribute, then your button will be disabled by default.
In .cs File
ButtonName.IsEnabled = false; If you set IsEnabled property as false for the button, it will disable the button

repeat button wpf not firing off click event

Hello community I have the following xaml.
<UserControl x:Class="Sample.SampleController"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="410" d:DesignWidth="324">
<UserControl.Resources>
<Style x:Key="buttonON" TargetType="RepeatButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="RepeatButton">
<Grid Width="80"
Height="80">
<Image Source="/Sample;component/icons/altitude_up_yellow.png"
Stretch="Fill" RenderOptions.BitmapScalingMode="Fant"/>
<ContentPresenter HorizontalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid Name="DriveGrid" Background="#C9000000" Width="324" Height="410">
<RepeatButton HorizontalAlignment="Left" x:Name="button1" VerticalAlignment="Top" IsEnabled="True" Style="{StaticResource buttonON}" Margin="232,297,0,0" Delay="100" Interval="200" Width="80" Height="80"/>
</Grid>
</UserControl>
And the following Code Behind
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Windows.Controls.Primitives;
namespace Sample {
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class SampleController : UserControl {
public SampleController() {
InitializeComponent();
button1.Click += new RoutedEventHandler(UpBtn_Click);
}
void UpBtn_Click(object sender, RoutedEventArgs e) {
Console.WriteLine("Click");
}
}
}
My question, why doesn't the repeat button fire off continously when I press and hold?
Thank you for your help.
To sum up comments. Even though I am not able to reproduce the problem stated in the question, as the code above works fine for me, I have found very similar issue in RepeatButton fires event only once! article. There is no definite answer what's causing the issue but it may give some clues to what to try

How to make PanelDragDropTarget act only as a target and not as a source?

I created a new solution. I added a link to System.Windows.Controls.Toolkit to my Silverlight project and wrote this code:
XAML:
<UserControl x:Class="SilverlightApplication4.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<Grid>
<toolkit:PanelDragDropTarget Margin="0,0,150,150" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" AllowedSourceEffects="Copy">
<Grid Name="grid1" Background="Blue">
<Rectangle Height="40" HorizontalAlignment="Left" Margin="5,5,0,0" Name="rectangle1" Stroke="Black" StrokeThickness="1" VerticalAlignment="Top" Width="80" Fill="Red" />
</Grid>
</toolkit:PanelDragDropTarget>
<toolkit:PanelDragDropTarget VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" Margin="150,150,0,0" AllowDrop="True" Drop="PanelDragDropTarget_Drop" AllowedSourceEffects="None">
<Grid Name="grid2" Background="Green" />
</toolkit:PanelDragDropTarget>
</Grid>
</Grid>
</UserControl>
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication4
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void PanelDragDropTarget_Drop(object sender, Microsoft.Windows.DragEventArgs e)
{
Rectangle myRectangle = new Rectangle() { Margin = new Thickness(5,5,0,0), Height = 40, Width = 80,
HorizontalAlignment = System.Windows.HorizontalAlignment.Left, VerticalAlignment = System.Windows.VerticalAlignment.Top,
StrokeThickness = 1, Stroke = new SolidColorBrush(Colors.Black), Fill = new SolidColorBrush(Colors.Red)};
grid2.Children.Add(myRectangle);
}
}
}
Now when I drag and drop the small red rectangle from grid1 onto grid2 everything works fine. But when I touch the new added rectangle in grid2 it shows visible signs that it can be dragged. My question is how to make a second PanelDragDropTarget (with grid2 inside) to act only as a target for drag and drop and not as a source? I mean how to block the possibility for a user to drag the new created rectangle in grid2, i.e. to exclude any visible signs that this new rectangle is draggable? Because it's not supposed to be draggable in my case.
I found a solution. For the PanelDragDropTarget adorner of the grid2 I defined an event handler for its ItemDragStarting event.
private void PanelDragDropTarget_ItemDragStarting(object sender, ItemDragEventArgs e)
{
e.Cancel = true;
e.Handled = true;
}
Now when I try to drag elements in grid2 nothing happens (that was my purpose).
Have you tried AllowedSourceEffects="None", works for me in SL5...

Resources