Question

It seems that my string is changing even if I don't change it. I'll explain:

if (globalCopy != null)
    {
       urlGlobal = globalCopy.Tag as string;
       urlGlobalOld = urlGlobal;
       if (String.IsNullOrEmpty(urlGlobal))
         {
           gta.Content = null;
           gta.Visibility = Visibility.Collapsed;
         }
    }

I made some kind of debug MessageBox further in the code, and when I call it it shows the following: globalCopy.Tag as string="somedata";urlGlobal="somedata";urlGlobalOld="". Problem is, that I don't change urlGlobal after this sequence (or at least it's not visible in the code for me), and I'm calling the debug msgBox also after this (I made the urlGlobalOld to see what data are stored there when entering the sequence). As you probably realized, the String.IsNullOrEmpty(urlGlobal) is always true and it shouldn't be. The whole code is here:

using System;
using System.Collections.Generic;
using System.Linq;
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.Shapes;
using System.ComponentModel;
using System.Xml;
using System.Threading;
using System.Globalization;

namespace AndyLaunchWPF
{
    /// <summary>
    /// Interaction logic for Store.xaml
    /// </summary>
    public partial class Store : Window
    {
        public Store()
        {
            InitializeComponent();
        }
        StackPanel itemElement;
        XmlDataProvider xmldp;
        //XmlDocument xmld;
        string selected = "All";
        ListBox CatList;
        string urlGlobal;
        int i = 0;
        Button globalCopy;
        string urlGlobalOld;
        private void AndyLaunch_Click(object sender, RoutedEventArgs e)
        {
            MainWindow main = new MainWindow();
            App.Current.MainWindow = main;
            this.Close();
            main.Show();
        }
        void Exit(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("The Application is going to be shutted down. Do you want to continue?", "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
            {
                Application.Current.Shutdown();
            }
        }
        private void lnkGoToArticle_Click(object sender, RoutedEventArgs e)
        {
            string url = (sender as Hyperlink).Tag as string;

            if (String.IsNullOrWhiteSpace(url)) return;

            System.Diagnostics.Process.Start(url);
        }

        private void lnkDownload_Click(object sender, RoutedEventArgs e)
        {
            string url = (sender as Button).Tag as string;

        if (String.IsNullOrWhiteSpace(url)) return;
        try { System.Diagnostics.Process.Start(url); }
        catch { MessageBox.Show("bad link"); }
    }
    void showCategory(string category)
    {
        //string se prida k nazvu xml elementu, All=>category=""
        //loadXMLDP("title"+category);
        //if (xmldp.Data.ToString() != "")
        //{
        //for (int i = 0; i == 2; i++)
        //{
        //int i=0;
        //while (i == 5)
        //{
        /*try { urlGlobal = globalCopy.Tag as string; }
        catch { }*/
            TextBlock added = new TextBlock();
            added = addItemTitle(category);
            try
            {

                itemElement.Children.Add(added);
                itemElement.RegisterName(added.Name, added);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Fail!\n" + ex.ToString(), ":(");
            }
            Label gta = new Label();
            Hyperlink goToArticle = new Hyperlink();
            goToArticle.Click += new RoutedEventHandler(lnkGoToArticle_Click);
            goToArticle.Inlines.Add(@">>");
            Binding goToArticleBinding = new Binding();
            goToArticleBinding.Path = new PropertyPath("InnerText");
            goToArticleBinding.XPath = "link";
            goToArticle.SetBinding(Hyperlink.TagProperty, goToArticleBinding);
            gta.Content = goToArticle;
            Binding gtaVis = new Binding();
            //updateUrl();
            /*try { urlGlobal = globalCopy.Tag as string; }
            catch { }*/



            //NullToVisibilityConverter ntvc;
            //gtaVis.Converter =

            gta.SetBinding(Label.VisibilityProperty, gtaVis);

            itemElement.Children.Add(gta);
            itemElement.RegisterName(goToArticle.Name, goToArticle);

            Button downloadButton = new Button();
            downloadButton.Name = "lnkDownload";
            downloadButton.Cursor = Cursors.Hand;
            downloadButton.Click += new RoutedEventHandler(lnkDownload_Click);
            Binding downloadButtonBinding = new Binding();


            downloadButtonBinding.XPath = "download" + category;
            downloadButtonBinding.Path = new PropertyPath("InnerText");

            downloadButton.SetBinding(Button.TagProperty, downloadButtonBinding);
            Style downloadButtonStyle = this.FindResource("NoChromeButton") as Style;
            downloadButton.Style = downloadButtonStyle;
            Button downloadButtonCopy = downloadButton;
            downloadButtonCopy.Loaded += new RoutedEventHandler(lnkDownloadCopy_Loaded);
            globalCopy = downloadButtonCopy;
            BitmapImage dbiBitmap = new BitmapImage();
            dbiBitmap.BeginInit();
            dbiBitmap.UriSource = new Uri("pack://application:,,,/AndyLaunchWPF;component/Images/download31.png");
            dbiBitmap.EndInit();
            Image dbi = new Image();
            dbi.Width = 30;
            dbi.Height = 30;
            dbi.Name = "downloadIcon";
            dbi.Source = dbiBitmap;
            downloadButton.Content = dbi;

            itemElement.Children.Add(downloadButton);
            itemElement.RegisterName(downloadButton.Name, downloadButton);
            urlGlobal = globalCopy.Tag as string;
            if (globalCopy != null)
            {
                urlGlobal = globalCopy.Tag as string;
                urlGlobalOld = urlGlobal;
                if (String.IsNullOrEmpty(urlGlobal))
                {
                    gta.Content = null;
                    gta.Visibility = Visibility.Collapsed;
                    //added.Visibility = Visibility.Collapsed;
                }
            }
        //}
            //}
            i++;
        //}
    }
    string updateUrl()
    {
        try { return globalCopy.Tag as string; }
        catch { return urlGlobal; }
    }
    void loadListBox()
    {
        //ItemsList.Items.Clear();

        try { selected = CategoriesList.SelectedItem.ToString(); }
        catch (Exception ex) { MessageBox.Show("Fail!\n" + ex.ToString(), ":("); }
        //if (selected==xmldp

        /*System.Xml.XmlDocument data = new System.Xml.XmlDocument();
        data.Load(@"http://www.andystore.bluefile.cz/?feed=rss2");
        xmldp.Document = data;
        xmldp.XPath = "//item";*/

        //addItemTitle("");
        showCategory("");


        //itemElement.Children.Add(dbi);
        //itemElement.RegisterName(dbi.Name, dbi);
    }
    public void updateListBox()
    {
        try { selected = CategoriesList.SelectedItem.ToString(); }
        catch (Exception ex) { MessageBox.Show("Fail!\n" + ex.ToString(), ":("); }

        itemElement.Children.Clear();
        if (selected == "System.Windows.Controls.ListBoxItem: All")
        {
            itemElement.InvalidateVisual();
            loadListBox();
            //addItemTitle();
            //showCategory("");
        }
        else if (selected == "System.Windows.Controls.ListBoxItem: Games")
        {
            itemElement.InvalidateVisual();
            showCategory("Games");

        }
        else if (selected == "System.Windows.Controls.ListBoxItem: Movies")
        {
            itemElement.InvalidateVisual();
            showCategory("Movies");
        }
        else
        {
            itemElement.InvalidateVisual();
            MessageBox.Show("Fail! Seems that this category doesn't exist", ":(");
        }

    }
    public TextBlock addItemTitle(string category)
    {
        Thickness mrg = new Thickness();
        mrg.Left = 2;
        mrg.Right = 2;
        mrg.Top = 2;
        mrg.Bottom = 2;
        TextBlock itemTitle = new TextBlock();
        itemTitle.Margin = mrg;

        itemTitle.FontSize = 16;
        itemTitle.VerticalAlignment = VerticalAlignment.Center;
        itemTitle.Text = "{Binding XPath=title}";
        itemTitle.FontWeight = FontWeights.Normal;
        itemTitle.Name = "itemTitle";
        Binding itemTitleBinding = new Binding();
        itemTitleBinding.XPath = "title"+category;
        itemTitle.SetBinding(TextBlock.TextProperty, itemTitleBinding);
        return itemTitle;
    }

    void itemElement_Loaded(object sender, RoutedEventArgs e)
    {
        itemElement = sender as StackPanel;
        loadListBox();

    }

    /*private void XmlDataProvider_DataChanged(object sender, EventArgs e)
    {
        xmldp = sender as XmlDataProvider;
    }*/

    private void CategoriesList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        //urlGlobal = globalCopy.Tag as string;
        CatList = new ListBox();
        //urlGlobal = globalCopy.Tag as string;
        updateListBox();
        //urlGlobal = globalCopy.Tag as string;
    }

    private void MenuItem_Click(object sender, RoutedEventArgs e)
    {
        updateListBox();
    }


    private void MenuItem_Click_1(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(selected);
    }

    void loadXMLDP(string path)
    {
        Thread.Sleep(50);
        xmldp.InitialLoad();
        /*try
        {*/
            xmldp.Source = new Uri("http://www.andystore.bluefile.cz/?feed=rss2");
        /*}
        catch(Exception ex)
        {
            MessageBox.Show("Fail!\n"+ex.ToString(), ":(");
        }
        try
        {*/
            xmldp.XPath = path;
        /*}
        catch (Exception ex)
        {
            MessageBox.Show("Fail!\n" + ex.ToString(), ":(");
        }*/
    }
    public sealed class NullToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value == null ? Visibility.Hidden : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    private void MenuItem_Click_2(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(Copy.Text);
    }

    private void lnkDownloadCopy_Loaded(object sender, RoutedEventArgs e)
    {
        urlGlobal = (sender as Button).Tag as string;

    }

    private void MenuItem_Click_3(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(i.ToString());
    }

    private void MenuItem_Click_4(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(globalCopy.Tag as string+"\nurlGlobal:"+urlGlobal+"\nurlGlobal was:"+urlGlobalOld);
    }
}
public class TextToVisibilityConverter : IValueConverter
{
    public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is string && targetType == typeof(Visibility))
        {
            if (value.ToString().Equals(string.Empty))
                return Visibility.Hidden;
            else
                return Visibility.Visible;
        }
        else
        {
            return null;
        }
    }

    public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is Visibility && targetType == typeof(string))
        {
            if ((Visibility)value == Visibility.Visible)
            {
                return "Text";
            }
            else
            {
                return string.Empty;
            }
        }
        else
        {
            return null;
        }
    }
}

}

Was it helpful?

Solution

You are setting urlGlobal within lnkDownloadCopy_Loaded(), so there's no reason it should be the same as urlGlobalOld which is set up in showCategory().

String.IsNullOrEmpty(urlGlobal) is true for a reason. urlGlobal is set via:

urlGlobal = globalCopy.Tag as string

globalCopy is a reference to downloadButtonCopy;

downloadButtonCopy is a reference to downloadButton.

downloadButton.Tag is set via a binding to the InnerText property of some XML that I gave up trying to trace and I doubt is even evaluated at that point.

Seriously, please use MVVM. The code is impossible to understand to the point where you clearly don't understand it yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top