private void _selectFromGrf_Click(object sender, RoutedEventArgs e)
        {
            try {
                MultiGrfExplorer dialog = new MultiGrfExplorer(_tab.ProjectDatabase.MetaGrf, EncodingService.FromAnyToDisplayEncoding(_grfPath2), ".bmp", EncodingService.FromAnyToDisplayEncoding(_textBox.Text));

                if (dialog.ShowDialog() == true)
                {
                    _textBox.Text = _ext == null?Path.GetFileName(dialog.SelectedPath.GetFullPath()) : Path.GetFileNameWithoutExtension(dialog.SelectedPath.GetFullPath());
                }
            }
            catch (Exception err) {
                ErrorHandler.HandleException(err);
            }
        }
        public ShopSimulatorDialog()
            : base("Shop simulator", "editor.png", SizeToContent.Height, ResizeMode.NoResize)
        {
            InitializeComponent();
            WindowStartupLocation = WindowStartupLocation.CenterOwner;
            Owner = WpfUtilities.TopWindow;

            _shopItems           = new RangeObservableCollection <ShopItem>();
            _lvItems.ItemsSource = _shopItems;

            Binder.Bind(_cbColorZeny, () => SdeAppConfiguration.UseZenyColors, () => _shopItems.ToList().ForEach(p => p.Update()));
            Binder.Bind(_cbDiscount, () => SdeAppConfiguration.UseDiscount, () => _shopItems.ToList().ForEach(p => p.Update()));
            Binder.Bind(_cbUseViewId, () => SdeAppConfiguration.AlwaysUseViewId, () => {
                if (!_enableEvents || _primaryShop == null)
                {
                    return;
                }

                try {
                    _enableEvents = false;

                    string viewId = _primaryShop.NpcViewId;
                    int ival;

                    if (SdeAppConfiguration.AlwaysUseViewId)
                    {
                        if (!Int32.TryParse(viewId, out ival))
                        {
                            var constDb = SdeEditor.Instance.ProjectDatabase.GetDb <string>(ServerDbs.Constants);
                            var tuple   = constDb.Table.TryGetTuple(viewId);

                            if (tuple != null)
                            {
                                ival = tuple.GetValue <int>(ServerConstantsAttributes.Value);
                            }
                            else
                            {
                                ival = -1;
                            }

                            _primaryShop.NpcViewId = ival.ToString(CultureInfo.InvariantCulture);
                            _tbNpcViewId.Text      = _primaryShop.NpcViewId;
                            _primaryShop.Reload();
                        }
                    }
                    else
                    {
                        if (Int32.TryParse(viewId, out ival))
                        {
                            viewId = _viewIdToString(ival);

                            if (!String.IsNullOrEmpty(viewId))
                            {
                                if (viewId.IsExtension(".act", ".spr"))
                                {
                                    _primaryShop.NpcViewId = Path.GetFileNameWithoutExtension(viewId.ToUpper());
                                }
                                else
                                {
                                    _primaryShop.NpcViewId = Path.GetFileName(viewId);
                                }

                                _tbNpcViewId.Text = _primaryShop.NpcViewId;
                                _primaryShop.Reload();
                            }
                        }
                    }
                }
                catch (Exception err) {
                    ErrorHandler.HandleException(err);
                }
                finally {
                    _enableEvents = true;
                }
            });

            _shop.TextChanged += new EventHandler(_shop_TextChanged);

            AvalonLoader.Load(_shop);
            WpfUtils.AddMouseInOutEffectsBox(_cbColorZeny, _cbDiscount, _cbUseViewId);

            _helper = new PreviewHelper(new RangeListView(), SdeEditor.Instance.ProjectDatabase.GetDb <int>(ServerDbs.Items), null, null, null, null);

            FrameViewerSettings settings = new FrameViewerSettings();

            settings.Act            = () => _act;
            settings.SelectedAction = () => _actIndexSelected.SelectedAction;
            settings.SelectedFrame  = () => _actIndexSelected.SelectedFrame;
            _frameViewer.InitComponent(settings);

            _actIndexSelected.Init(_frameViewer);
            _actIndexSelected.Load(_act);
            _actIndexSelected.FrameChanged  += (s, p) => _frameViewer.Update();
            _actIndexSelected.ActionChanged += (s, p) => {
                _frameViewer.Update();

                if (!_enableEvents || _primaryShop == null)
                {
                    return;
                }

                try {
                    _enableEvents = false;

                    var elements = _tbNpcPosition.Text.Split(',');
                    var dir      = _convertAction(_actIndexSelected.SelectedAction);

                    if (elements.Length == 4)
                    {
                        elements[3] = dir.ToString(CultureInfo.InvariantCulture);
                        _primaryShop.ShopLocation = string.Join(",", elements);
                        _primaryShop.Reload();
                    }
                    else
                    {
                        _primaryShop.ShopLocation = "prontera,150,150," + dir.ToString(CultureInfo.InvariantCulture);
                        _primaryShop.Reload();
                    }

                    _tbNpcPosition.Text = _primaryShop.ShopLocation;
                }
                catch (Exception err) {
                    ErrorHandler.HandleException(err);
                }
                finally {
                    _enableEvents = true;
                }
            };

            _actIndexSelected.SpecialFrameChanged += (s, p) => _frameViewer.Update();

            _lvItems.MouseRightButtonUp += new MouseButtonEventHandler(_lvItems_MouseRightButtonUp);
            _lvItems.SelectionChanged   += new SelectionChangedEventHandler(_lvItems_SelectionChanged);

            _tbItemId.TextChanged          += new TextChangedEventHandler(_tbItemId_TextChanged);
            _tbPrice.TextChanged           += new TextChangedEventHandler(_tbPrice_TextChanged);
            _tbNpcViewId.TextChanged       += new TextChangedEventHandler(_tbNpcViewId_TextChanged);
            _tbNpcPosition.TextChanged     += new TextChangedEventHandler(_tbNpcPosition_TextChanged);
            _tbNpcDisplayName.TextChanged  += new TextChangedEventHandler(_tbNpcDisplayName_TextChanged);
            _tbNpcShopCurrency.TextChanged += new TextChangedEventHandler(_tbNpcShopCurrency_TextChanged);

            _comboBoxShopType.SelectionChanged += new SelectionChangedEventHandler(_comboBoxShopType_SelectionChanged);

            _buttonResetPrice.Click += delegate {
                _tbPrice.Text = "-1";
            };

            _buttonCurItemQuery.Click += delegate {
                try {
                    Table <int, ReadableTuple <int> > btable = SdeEditor.Instance.ProjectDatabase.GetMetaTable <int>(ServerDbs.Items);

                    SelectFromDialog select = new SelectFromDialog(btable, ServerDbs.Items, _tbNpcShopCurrency.Text);
                    select.Owner = WpfUtilities.TopWindow;

                    if (select.ShowDialog() == true)
                    {
                        _tbNpcShopCurrency.Text = select.Id;
                    }
                }
                catch (Exception err) {
                    ErrorHandler.HandleException(err);
                }
            };

            _buttonViewIdQuery.Click += delegate {
                try {
                    MultiGrfExplorer dialog = new MultiGrfExplorer(SdeEditor.Instance.ProjectDatabase.MetaGrf, "data\\sprite\\npc", "", _viewIdToString(FormatConverters.IntOrHexConverter(_primaryShop.NpcViewId ?? "")));

                    if (dialog.ShowDialog() == true)
                    {
                        var    path = dialog.SelectedPath.GetFullPath();
                        string result;

                        if (path.IsExtension(".act", ".spr"))
                        {
                            result = Path.GetFileNameWithoutExtension(path.ToUpper());
                        }
                        else
                        {
                            result = Path.GetFileName(path);
                        }

                        if (SdeAppConfiguration.AlwaysUseViewId)
                        {
                            var constDb = SdeEditor.Instance.ProjectDatabase.GetDb <string>(ServerDbs.Constants);
                            var tuple   = constDb.Table.TryGetTuple(result);
                            int ival;

                            if (tuple != null)
                            {
                                ival = tuple.GetValue <int>(ServerConstantsAttributes.Value);
                            }
                            else
                            {
                                _tbNpcViewId.Text = result;
                                return;
                            }

                            if (!_enableEvents || _primaryShop == null)
                            {
                                return;
                            }

                            try {
                                _enableEvents = false;

                                _primaryShop.NpcViewId = ival.ToString(CultureInfo.InvariantCulture);
                                _tbNpcViewId.Text      = _primaryShop.NpcViewId;
                                _updateViewShop();
                                _primaryShop.Reload();
                            }
                            catch (Exception err) {
                                ErrorHandler.HandleException(err);
                            }
                            finally {
                                _enableEvents = true;
                            }
                        }
                        else
                        {
                            _tbNpcViewId.Text = result;
                        }
                    }
                }
                catch (Exception err) {
                    ErrorHandler.HandleException(err);
                }
            };

            _buttonQueryItem.Click += delegate {
                try {
                    Table <int, ReadableTuple <int> > btable = SdeEditor.Instance.ProjectDatabase.GetMetaTable <int>(ServerDbs.Items);

                    SelectFromDialog select = new SelectFromDialog(btable, ServerDbs.Items, _tbItemId.Text);
                    select.Owner = WpfUtilities.TopWindow;

                    if (select.ShowDialog() == true)
                    {
                        _tbItemId.Text = select.Id;
                    }
                }
                catch (Exception err) {
                    ErrorHandler.HandleException(err);
                }
            };

            _gridColumnPrimary.Width = new GridLength(230 + SystemParameters.VerticalScrollBarWidth + 7);

            this.Loaded += delegate {
                this.MinHeight  = this.ActualHeight + 10;
                this.MinWidth   = this.ActualWidth;
                this.ResizeMode = ResizeMode.CanResize;
                SizeToContent   = SizeToContent.Manual;
            };

            ApplicationShortcut.Link(ApplicationShortcut.Delete, () => _buttonDelete_Click(null, null), _lvItems);
            ApplicationShortcut.Link(ApplicationShortcut.New, () => _buttonNew_Click(null, null), this);
            ApplicationShortcut.Link(ApplicationShortcut.FromString("Ctrl-Up", "MoveUp"), () => _buttonUp_Click(null, null), _lvItems);
            ApplicationShortcut.Link(ApplicationShortcut.FromString("Ctrl-Down", "MoveDown"), () => _buttonDown_Click(null, null), _lvItems);
            ApplicationShortcut.Link(ApplicationShortcut.Undo, () => _undo(), this);
            ApplicationShortcut.Link(ApplicationShortcut.Redo, () => _redo(), this);
            //_shop.Text = "alberta_in,182,97,0	shop	Tool Dealer#alb2	73,1750:-1,611:-1,501:-1,502:-1,503:-1,504:-1,506:-1,645:-1,656:-1,601:-1,602:-1,2243:-1";
        }