ratishphilip / wpfspark

A rich UserControl library to enhance the look and feel of WPF applications.
http://www.codeproject.com/Articles/1060961/WPFSpark-v
MIT License
548 stars 84 forks source link

FluidWrapPanel Automatic layout problem #3

Closed dandanyouxiang closed 8 years ago

dandanyouxiang commented 8 years ago

I want to know why he didn't fill the entire parent window after I had been dragged.

ratishphilip commented 8 years ago

Could you provide more detail about your issue?

dandanyouxiang commented 8 years ago

@ratishphilip When I switch the position of the child controls, they are not filled with the entire container I want to upload a png but "Something went really wrong, and we can’t process that file."

ratishphilip commented 8 years ago

@dandanyouxiang FluidWrapPanel does not automatically change the size of its children. It only manages their position.

You have to write your own logic to resize them. Check my blog for more details.

dandanyouxiang commented 8 years ago

@ratishphilip thanks,I do not automatically change the size of its children.I just want to change their position.But they don't fill the container after the adjustment

dandanyouxiang commented 8 years ago

@ratishphilip Could you give me a Email,I want to send you two pictures to show you the results。I split this FluidWrapPanel three rows and three columns,A large child occupy two rows and two columns,Five small child each occupy one line and one column,When I have exchange of their location, the big will run out of the FluidWrapPanel ,I want to kown why?Look forward to your reply

ratishphilip commented 8 years ago

Can you please share your images on OneDrive or though any image sharing site? I did a quick sample UWP project based on your scenario. It seems to be working fine. I have attached the source code. Kindly have a look. TestFWP2.zip

dandanyouxiang commented 8 years ago

@ratishphilip I want to upload a png but "Something went really wrong, and we can’t process that file." And I can't open your Uri (TestFWP2.zip)

dandanyouxiang commented 8 years ago

@ratishphilip I used a wpf project

ratishphilip commented 8 years ago

@dandanyouxiang I sent you the WPF version to your mail id

You can send me your images to 2233475@guerrillamail.com.

dandanyouxiang commented 8 years ago

@ratishphilip I have sent you a Email please check

ratishphilip commented 8 years ago

@dandanyouxiang I found out the causes of this issue.

  1. You are not setting any specific width to the FluidWrapPanel, so it takes the dimensions of its parent which is 600x600. Now the size of the small squares is 100x100. So 3 rows and 3 columns would require the FluidWrapPanel to be of size 300x300. So when you try to rearrange the squares, the larger square goes below because the FluidWrapPanel assumes it has 600x600 area to rearrange the squares and does so. So you have to set the Width and Height of the FluidWrapPanel to 300.
  2. In Main.xaml.cs, in the RefreshFluidWrapPanel() method you are doing the following
var brush = _brushes[_random.Next(_brushes.Length)];
//var factor = 1;
var factorWidth = UseRandomChildSize ? _random.Next(1, 3) : 1;
var factorHeight = UseRandomChildSize ? _random.Next(1, 3) : 1;

var factorWidth = 1;
var factorHeight = 1;

if (i ==0)
{
    panel.ItemWidth = commWidth * 2;
    panel.ItemHeight = commHeight * 2;
}
else
{
    panel.ItemWidth = commWidth * 1;
    panel.ItemHeight = commHeight * 1;
}

var ctrl = new FluidItemControl
{
    ClipToBounds=true,
    Width = factorWidth * panel.ItemWidth,
    Height = factorHeight * panel.ItemHeight,
    Fill = brush,
    Data = (i + 1).ToString()
};
ctrl.SetValue(Panel.ZIndexProperty, 2);
items.Add(ctrl);

Here if I==0 then you are modifying the ItemWidth and ItemHeight of the FluidWrapPanel (which you should not because you have already set it in the Main.xaml file) Also you are setting the Width and Height of the FluidItemControl to factorWidth * ItemWidth and factorHeight * ItemHeight which is not the correct logic here.

The correct code should be

private void RefreshFluidWrapPanel()
{
    var items = new ObservableCollection<UIElement>();
    var count = 6;
    for (var i = 0; i < count; i++)
    {
        var brush = _brushes[_random.Next(_brushes.Length)];
        var factorWidth = 1;
        var factorHeight = 1;

        if (i ==0)
        {
            factorWidth = 2;
            factorHeight = 2;
        }

        var ctrl = new FluidItemControl
        {
            ClipToBounds=true,
            Width = factorWidth * panel.ItemWidth,
            Height = factorHeight * panel.ItemHeight,
            Fill = brush,
            Data = (i + 1).ToString()
        };
        ctrl.SetValue(Panel.ZIndexProperty, 2);
        items.Add(ctrl);
    }

    panel.ItemsSource = items;
}

If you do these two changes, your panel shall work as expected.

ratishphilip commented 8 years ago

Here is the corrected code CusPanel.zip

dandanyouxiang commented 8 years ago

@ratishphilip can you give send email ,I can not download this “”CusPanel.zip”

dandanyouxiang commented 8 years ago

@ratishphilip I tried your way ,but do not work;

ratishphilip commented 8 years ago

I sent it via mail to 2896232010@qq.com

dandanyouxiang commented 8 years ago

@ratishphilip I was to divide the window into three rows and three columns.Let them stained with the whole window,not you give me

ratishphilip commented 8 years ago

Ok. I am able to reproduce your issue here. This might require some change in the FluidWrapPanel logic. I am looking into it.

dandanyouxiang commented 8 years ago

thank you,Look forward to your reply

ratishphilip commented 8 years ago

@dandanyouxiang I have updated the Layout logic of FluidWrapPanel with a more robust code. Do try it. It should now work as per your requirement. You can find the latest package (v1.3) available in Nuget.

P.S. : I have updated the Target DotNetFramework to 4.6.2. So you may have to retarget your project to 4.6.2 too.

dandanyouxiang commented 8 years ago

I tested no problem.Thank you

ratishphilip commented 8 years ago

You are welcome!

dandanyouxiang commented 8 years ago

@ratishphilip I want to say that you write well

ratishphilip commented 8 years ago

@dandanyouxiang Thank you very much.