listview drag

This commit is contained in:
2024-01-28 23:10:11 +02:00
parent 7e04b83728
commit fe756648de
5 changed files with 164 additions and 15 deletions
+6 -7
View File
@@ -1,5 +1,4 @@
using System.Windows;
using System.Windows.Controls.Primitives;
using TV_Player.ViewModels;
namespace TV_Player
@@ -10,13 +9,13 @@ namespace TV_Player
public partial class App : Application
{
private TVPlayerViewModel _tvPlayer;
private const string Guid = "250C5597-BA73-40DF-B2CF-DD644F044834";
private const string Guid = "ac8ab758-01e2-47c3-ad42-31e96d8203c0";
static readonly Mutex Mutex = new Mutex(true, "{" + Guid + "}");
protected override void OnStartup(StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException += ReportAndRestart;
if (!Mutex.WaitOne(TimeSpan.Zero, true))
{
MessageBox.Show("Programm already running");
@@ -32,13 +31,13 @@ namespace TV_Player
{
string info = e.ExceptionObject.ToString();
MessageBox.Show(info,"Application");
var result=MessageBox.Show(info, "Application", MessageBoxButton.OK, MessageBoxImage.Stop);
Environment.Exit(1);
//Environment.Exit(1);
}
protected override void OnExit(ExitEventArgs e)
{
_tvPlayer = null;
_tvPlayer.Dispose();
base.OnExit(e);
}
}
+23 -3
View File
@@ -68,7 +68,7 @@ c-0.781-0.781-0.788-2.047-0.007-2.828L51.438,14.43c1.754-1.755,1.753-4.61-0.001-
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ButtonDown" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
@@ -123,6 +123,7 @@ c-0.781-0.781-0.788-2.047-0.007-2.828L51.438,14.43c1.754-1.755,1.753-4.61-0.001-
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ListBoxView" TargetType="ListView">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="VerticalAlignment" Value="Top"/>
@@ -136,7 +137,7 @@ c-0.781-0.781-0.788-2.047-0.007-2.828L51.438,14.43c1.754-1.755,1.753-4.61-0.001-
<Setter Property="VirtualizingPanel.IsContainerVirtualizable" Value="True" />
<Style.Resources>
<Style x:Key="ScrollThumbs" TargetType="{x:Type Thumb}">
<Setter Property="Background" Value="#3E85C3" />
<Setter Property="Background" Value="Gray" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
@@ -165,7 +166,7 @@ c-0.781-0.781-0.788-2.047-0.007-2.828L51.438,14.43c1.754-1.755,1.753-4.61-0.001-
<Style TargetType="{x:Type ScrollBar}">
<Setter Property="Margin" Value="0"></Setter>
<Setter Property="Padding" Value="0"></Setter>
<Setter Property="Background" Value="#ecf0f6" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="Width" Value="19" />
<Setter Property="Template">
<Setter.Value>
@@ -196,4 +197,23 @@ c-0.781-0.781-0.788-2.047-0.007-2.828L51.438,14.43c1.754-1.755,1.753-4.61-0.001-
</Style>
</Style.Resources>
</Style>
<Style x:Key="ItemContStyle" TargetType="{x:Type ListViewItem}">
<!-- Stretch the content so that we can right-align values
in the Balance column. -->
<Setter Property="Margin" Value="2,2,2,2" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border x:Name="DGR_Border">
<GridViewRowPresenter x:Name="gridrowPresenter"
Content="{TemplateBinding Property=ContentControl.Content}"
/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
+11 -2
View File
@@ -6,9 +6,12 @@
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid x:Name="programsGrid">
<ListView ItemsSource="{Binding Programs}" Background="Transparent" VerticalAlignment="Top" Margin="0,50,0,0"
<ListView x:Name="programsList" ItemsSource="{Binding Programs}" Background="Transparent" VerticalAlignment="Top" Margin="0,50,0,0"
SelectionMode="Single" Style="{StaticResource ListBoxView}"
SelectedItem="{Binding SelectedItem}" BorderThickness="0"
PreviewMouseDown="ListView_PreviewMouseDown"
PreviewMouseUp="ListView_PreviewMouseUp"
PreviewMouseMove="ListView_MouseMove"
SelectionChanged="ListBox_SelectionChanged">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
@@ -18,7 +21,7 @@
<ListView.ItemTemplate>
<DataTemplate>
<Viewbox HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Border Height="145" Width="106" HorizontalAlignment="Center" CornerRadius="5" BorderBrush="Yellow" BorderThickness="2">
<Border x:Name="border" Height="145" Width="106" HorizontalAlignment="Center" CornerRadius="5" BorderBrush="Yellow" BorderThickness="2">
<Grid Margin="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="#B0000000">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
@@ -31,6 +34,12 @@
</Grid>
</Border>
</Viewbox>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}}" Value="True">
<Setter TargetName="border" Property="Background" Value="red"/>
<!-- Add other properties or setters as needed -->
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
+118 -2
View File
@@ -1,4 +1,8 @@
using System.Windows.Controls;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
namespace TV_Player
{
@@ -7,16 +11,128 @@ namespace TV_Player
/// </summary>
public partial class ProgramsList : UserControl
{
private Point _mousePosition;
private bool _isDragging;
private DispatcherTimer clickTimer;
public ProgramsList()
{
InitializeComponent();
clickTimer = new DispatcherTimer();
clickTimer.Interval = TimeSpan.FromMilliseconds(200); // Adjust the interval as needed
clickTimer.Tick += ClickTimer_Tick;
}
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (DataContext is ProgramsListViewModel viewModel)
if (_isDragging)
{
e.Handled = true; // Prevent selection change during drag-and-drop
}
else if (DataContext is ProgramsListViewModel viewModel)
{
viewModel.ItemSelectedCommand.Execute(null);
}
}
private void ListView_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
_isDragging = true;
_mousePosition = e.GetPosition(null);
}
private void ListView_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
Point currentPosition = e.GetPosition(null);
if (!_isDragging && (Math.Abs(currentPosition.X - _mousePosition.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(currentPosition.Y - _mousePosition.Y) > SystemParameters.MinimumVerticalDragDistance))
{
_isDragging = true;
e.Handled = true;
}
if (_isDragging)
{
// Get the ListView scroll viewer
ScrollViewer scrollViewer = FindVisualChild<ScrollViewer>(programsList);
if (scrollViewer != null)
{
double verticalScrollPosition = currentPosition.Y - _mousePosition.Y;
scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - verticalScrollPosition);
}
}
}
}
private void ListView_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
_isDragging = false;
clickTimer.Stop();
if (IsClick())
{
// Perform selection logic here
ListViewItem listViewItem = FindAncestor<ListViewItem>((DependencyObject)e.OriginalSource);
if (listViewItem != null)
{
listViewItem.IsSelected = true;
if (DataContext is ProgramsListViewModel viewModel)
{
viewModel.ItemSelectedCommand.Execute(null);
}
}
}
}
private void ClickTimer_Tick(object sender, EventArgs e)
{
clickTimer.Stop();
}
private bool IsClick()
{
Point currentPosition = Mouse.GetPosition(null);
return Math.Abs(currentPosition.X - _mousePosition.X) < SystemParameters.MinimumHorizontalDragDistance &&
Math.Abs(currentPosition.Y - _mousePosition.Y) < SystemParameters.MinimumVerticalDragDistance;
}
private static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is T)
{
return (T)child;
}
else
{
T childOfChild = FindVisualChild<T>(child);
if (childOfChild != null)
{
return childOfChild;
}
}
}
return null;
}
private static T FindAncestor<T>(DependencyObject current) where T : DependencyObject
{
do
{
if (current is T ancestor)
{
return ancestor;
}
current = VisualTreeHelper.GetParent(current);
} while (current != null);
return null;
}
}
}
@@ -2,7 +2,7 @@
namespace TV_Player.ViewModels
{
public class TVPlayerViewModel
public class TVPlayerViewModel : IDisposable
{
private readonly MainViewModel _mainViewModel;
@@ -66,5 +66,10 @@ namespace TV_Player.ViewModels
_mainViewModel.Control = control;
}
public void Dispose()
{
if (_mainViewModel.Control is IDisposable disposable)
disposable.Dispose();
}
}
}