Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 984971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:05:45+00:00 2026-05-16T05:05:45+00:00

I’m working on dragging objects around a Canvas, which are encapsulated in ListBoxItems —

  • 0

I’m working on dragging objects around a Canvas, which are encapsulated in ListBoxItems — the effect being to create a simple pseudo desktop.

I have a ListBox with a Canvas as the ItemsPanelTempalte, so that the ListBoxItems can appear anywhere on screen:

<ListBox ItemsSource="{Binding Windows}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

I have a Style to define how the ListBoxItems should appear:

<Style TargetType="{x:Type ListBoxItem}">
    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
    <Setter Property="Canvas.Left" Value="{Binding Left, Mode=TwoWay}" />
    <Setter Property="Canvas.Top" Value="{Binding Top, Mode=TwoWay}" />
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <local:PseudoWindowContainer Content="{TemplateBinding Content}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

The “PseudoWindowContainer” extends from the ContentControl and has its own Style applied to make it look like a dialog box (title bar, close button, etc…). Here is a chunk of it:

  <Style TargetType="{x:Type local:PseudoWindowContainer}">
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
<Setter Property="Width" Value="{Binding Width, Mode=TwoWay}" />
<Setter Property="Height" Value="{Binding Height, Mode=TwoWay}" />
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Template">
  <Setter.Value>
    <ControlTemplate TargetType="{x:Type local:PseudoWindowContainer}">
      <Grid Name="LayoutRoot" Background="White">
        <!-- ... snip ... -->
            <Border Name="PART_TitleBar" Grid.Row="0" Background="LightGray" CornerRadius="2,2,0,0" VerticalAlignment="Stretch" Cursor="Hand" />
            <TextBlock Name="TitleBar_Caption" Text="{Binding DisplayName}" Grid.Row="0" Background="Transparent" Padding="5,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Center" />
            <Button Name="TitleBar_CloseButton" Command="{Binding CloseCommand}" Grid.Row="0" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,5,5,0" Width="20" Height="20" Cursor="Hand" Background="#FFFF0000" Foreground="#FF212121" />
            <!-- ContentPresenter -->
            <ContentPresenter Grid.Row="1" />
        <!-- ... snip ... -->
      </Grid>
      <ControlTemplate.Triggers>
        <Trigger Property="IsSelected" Value="True">
          <Setter TargetName="WindowBorder" Property="Background" Value="Blue" />
        </Trigger>
        <Trigger Property="IsSelected" Value="False">
          <Setter TargetName="WindowBorder" Property="Background" Value="#22000000" />
        </Trigger>
      </ControlTemplate.Triggers>
    </ControlTemplate>
  </Setter.Value>
</Setter>

Inside the PseudoWindowContainer.cs class I create some event handlers to listen for MouseDown/MouseUp/MoveMove events:

public override void OnApplyTemplate()
{
  _titleBar = (Border)Template.FindName("PART_TitleBar", this);
  if (_titleBar != null)
  {
    _titleBar.MouseDown += TitleBar_MouseDown;
    _titleBar.MouseUp += TitleBar_MouseUp;
  }

  _grip = (ResizeGrip)Template.FindName("PART_ResizeGrip", this);
  if (_grip != null)
  {
    _grip.MouseLeftButtonDown += ResizeGrip_MouseLeftButtonDown;
    _grip.MouseLeftButtonUp += ResizeGrip_MouseLeftButtonUp;
  }

  base.OnApplyTemplate();
}

private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
{
  _titleBar.MouseMove += TitleBar_MouseMove;
  ((Border)sender).CaptureMouse();

  _windowLocation.X = Left;
  _windowLocation.Y = Top;

  _clickLocation = this.PointToScreen(Mouse.GetPosition(this));
}

private void TitleBar_MouseUp(object sender, MouseButtonEventArgs e)
{
  _titleBar.MouseMove -= TitleBar_MouseMove;
  ((Border)sender).ReleaseMouseCapture();
}

private void TitleBar_MouseMove(object sender, MouseEventArgs e)
{
  Point currentLocation = this.PointToScreen(Mouse.GetPosition(this));

  Left = _windowLocation.X + currentLocation.X - _clickLocation.X;
  Top = _windowLocation.Y + currentLocation.Y - _clickLocation.Y;
}

The trouble I run into is the “Left” and “Top” are not defined properties, and updating them to Canvas.SetLeft/SetTop (or GetLeft/GetTop, accordingly) does not update the position on the Canvas.

I have “Left” and “Top” defined in the ViewModel of the controls I place into the ListBoxItems, and are thus subsequently wrapped with a PseudoWindowContainer because of the Template. These values are being honored and the objects do appear in the correct location when the application comes originally.

I believe I need to somehow define “Left” and “Top” in my PseudoWindowContainer (aka: ContentControl) and have them propagate back up to my ViewModel. Is this possible?

Thanks again for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:05:45+00:00Added an answer on May 16, 2026 at 5:05 am

    I’ve found a solution to the problem. Instead of typing it all out again, I will point to the MSDN Forum post that describes what I did:
    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/d9036b30-bc6e-490e-8f1e-763028a50153

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.