Click here to Skip to main content
14,977,931 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: Application download error in XBAP application after deploying the application Pin
indian14321-Sep-16 13:54
Memberindian14321-Sep-16 13:54 
GeneralRe: Application download error in XBAP application after deploying the application Pin
Richard Deeming22-Sep-16 1:46
mveRichard Deeming22-Sep-16 1:46 
QuestionUsing a static class to call child Usercontrols functions from a parent window button click event Pin
holdorf19-Sep-16 5:55
Memberholdorf19-Sep-16 5:55 
AnswerRe: Using a static class to call child Usercontrols functions from a parent window button click event Pin
Gerry Schmitz19-Sep-16 7:14
mveGerry Schmitz19-Sep-16 7:14 
GeneralRe: Using a static class to call child Usercontrols functions from a parent window button click event Pin
holdorf19-Sep-16 8:36
Memberholdorf19-Sep-16 8:36 
GeneralRe: Using a static class to call child Usercontrols functions from a parent window button click event Pin
Gerry Schmitz19-Sep-16 10:36
mveGerry Schmitz19-Sep-16 10:36 
GeneralRe: Using a static class to call child Usercontrols functions from a parent window button click event Pin
incipire12-Oct-16 9:06
professionalincipire12-Oct-16 9:06 
QuestionSilverlight listbox is keeping the focus on more than one items Pin
indian14318-Sep-16 18:24
Memberindian14318-Sep-16 18:24 
Hi All,

I am using a Silverlight listbox on which I display some integers, when I select a particular item of listbox (ie integer), it has to show me the value of that integer. Showing part is working perfect but the problem is when I select one item and then go to another item, silverligh is not clearing the selection of the first one instead keeping the selection on both, which looks weird to customer, any help is appreciated.

Here is my xaml
<UserControl x:Class="RollDiceApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="300"
    xmlns:local="clr-namespace:RollDiceApp"
    xmlns:viewmodel="clr-namespace:RollDiceApp.ViewModel" 
    Height="650" Width="700" 
    Name="diceGamePage"

>
    <UserControl.DataContext>
        <viewmodel:DiceGameViewModel x:Name="_diceGameViewModel" 
             />
    </UserControl.DataContext>

<pre>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition>
        </ColumnDefinition>
        <ColumnDefinition Width="76.988"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <ListBox x:Name="lblHistory" Margin="1.988,40.96,10,10" Grid.Column="1" FontSize="21.333"
             FontWeight="Bold" FontStyle="Italic"
             ItemsSource="{Binding Items}"
             SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}" SelectionMode="Single" >
    </ListBox>
    <Button x:Name="cmdRollDice" Content="Roll Dice" VerticalAlignment="Top" Margin="12,13,0,0" HorizontalAlignment="Left" Width="65"
       Command="{Binding Path=ClickSaveCommand}" >
    </Button>

    <Button x:Name="cmdClearAllDice" Content="Clear All Dice" VerticalAlignment="Top" Margin="610,13,-2,0" HorizontalAlignment="Left" Width="92"
       Command="{Binding Path=ClearHistoryCommand}" Grid.ColumnSpan="2" >
    </Button>

    <Image x:Name="imgDice" Margin="10, 42.97,10,10" Source="{Binding ImageSourcePath}" />
</Grid>




Here is my ViewModel
namespace RollDiceApp.ViewModel
{
    public class DiceGameViewModel : INotifyPropertyChanged, IDataErrorInfo
    {
        public DiceGameViewModel()
        {
            _items = new ObservableCollection<int>();
            _imageSourcePath = string.Empty;// GetNewImagePath(GetRandom(1, 6));
        }

        private string GetNewImagePath(int v)
        {
            if ((v == null) || (v <= 0))
                v = 1;           

            return HelperMethods.GetRootDirectory() + @"/Images/" + v.ToString() + ".jpg";
        }

        private int GetRandom(int min, int max)
        {
            Random generator = new Random();
            return generator.Next(min, max);
        }

        private string _imageSourcePath;
        public  string ImageSourcePath {
            get { return _imageSourcePath; }
            set
            {
                _imageSourcePath = value;
                NotifyPropertyChanged("ImageSourcePath");
            }
        }

        private ObservableCollection<int> _items;
        public ObservableCollection <int> Items
        {
            get { return _items; }
            set
            {
                _items = value;
                NotifyPropertyChanged("Items");
            }
        }

        private int _selecteditem;
        public int SelectedItem {
            get { return _selecteditem; }
            set
            {<br />
                _selecteditem = value;
                NotifyPropertyChanged("SelectedItem");
                DisplayNewDiceValue(value);
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if(null!=handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void DisplayNewDiceValue(int val)
        {
            ImageSourcePath = GetNewImagePath(val);
            NotifyPropertyChanged("ImageSourcePath");
        }
        private void DisplayHistory(int val)
        {
            _items.Add(val);
            NotifyPropertyChanged("ImageSourcePath");
        }

        private void ClickRollDice()
        {
            int i = GetRandom(1, 6);
            DisplayNewDiceValue(i);
            DisplayHistory(i);
        }
        private void CmdClearHistory()
        {
            _items.Clear();
            ImageSourcePath = "";
            NotifyPropertyChanged("ImageSourcePath");
        }

        private ICommand _clickSaveCommand;
        public ICommand ClickSaveCommand {
            get { return _clickSaveCommand ?? 
                    (_clickSaveCommand = new RollDiceApp.ViewModel.Command.RollDiceCommand(() => ClickRollDice(), true));
            } }

        private ICommand _clearHistoryCommand;
        public ICommand ClearHistoryCommand
        {
            get
            {
                return _clickSaveCommand ??
                  (_clickSaveCommand = new RollDiceApp.ViewModel.Command.RollDiceCommand(() => CmdClearHistory(), true));
            }
        }

        string IDataErrorInfo.this[string columnName]
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        string IDataErrorInfo.Error
        {
            get
            {
                throw new NotImplementedException();
            }
        }       
    }
}

Here is my Command
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace RollDiceApp.ViewModel.Command
{
    public class RollDiceCommand : ICommand
    {
        private Action _action;
        private Boolean _canExecute;

        public RollDiceCommand(Action _action, Boolean _canExecute)
        {
            this._action = _action;
            this._canExecute = _canExecute;
        }

        bool ICommand.CanExecute(object parameter)
        {
            return _canExecute;
        }

        public event EventHandler CanExecuteChanged;
        void ICommand.Execute(object parameter)
        {
            _action();
        }
    }
}

Here is my other class
namespace RollDiceApp
{
    public class HelperMethods
    {
        public static string GetRootDirectory()
        {

            string uriPath = Application.Current.Host.Source.ToString();
            Uri uri = new Uri(uriPath);
            Uri parent = new Uri(uri, "..");          
            return parent.ToString();
        }
    }
}

Here is my App.xaml
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             x:Class="RollDiceApp.App"

<pre>
         >
<Application.Resources>

</Application.Resources>




There are 6 images in images folder with names 1.jpg to 6.jpg, you can create any Images for that.
Thanks,

Abdul Aleem

"There is already enough hatred in the world lets spread love, compassion and affection."

QuestionHow can I call xbap from client browser Pin
indian14316-Sep-16 6:57
Memberindian14316-Sep-16 6:57 
QuestionWPF Expander Pin
Itamar Gigi12-Sep-16 0:22
MemberItamar Gigi12-Sep-16 0:22 
AnswerRe: WPF Expander Pin
Pete O'Hanlon12-Sep-16 2:22
mvePete O'Hanlon12-Sep-16 2:22 
GeneralRe: WPF Expander Pin
Itamar Gigi12-Sep-16 2:36
MemberItamar Gigi12-Sep-16 2:36 
GeneralRe: WPF Expander Pin
Pete O'Hanlon12-Sep-16 2:58
mvePete O'Hanlon12-Sep-16 2:58 
GeneralRe: WPF Expander Pin
Itamar Gigi12-Sep-16 3:09
MemberItamar Gigi12-Sep-16 3:09 
GeneralRe: WPF Expander Pin
Itamar Gigi12-Sep-16 2:39
MemberItamar Gigi12-Sep-16 2:39 
GeneralRe: WPF Expander Pin
Itamar Gigi12-Sep-16 2:45
MemberItamar Gigi12-Sep-16 2:45 
QuestionDynamic ContentControl > Style > DataTemplate....binding problem! Pin
Jayme658-Sep-16 6:46
MemberJayme658-Sep-16 6:46 
QuestionListView SelectionChanged event not firing for the first already selected row. Pin
holdorf7-Sep-16 7:40
Memberholdorf7-Sep-16 7:40 
AnswerRe: ListView SelectionChanged event not firing for the first already selected row. Pin
holdorf9-Sep-16 5:52
Memberholdorf9-Sep-16 5:52 
QuestionWPF Listbox - How to keep the scrolling from changing the property? Pin
dbrenth6-Sep-16 3:49
Memberdbrenth6-Sep-16 3:49 
QuestionDoes width and height need to be explicitly defined in ControlTemplate? Pin
James J M5-Sep-16 6:35
MemberJames J M5-Sep-16 6:35 
QuestionComboBox - SelectedValue and SelectedItem Pin
Mycroft Holmes31-Aug-16 14:48
professionalMycroft Holmes31-Aug-16 14:48 
QuestionWPF: Reading a document's bytes into a stream to store in a database table. Pin
holdorf31-Aug-16 10:47
Memberholdorf31-Aug-16 10:47 
AnswerRe: WPF: Reading a document's bytes into a stream to store in a database table. Pin
Pete O'Hanlon31-Aug-16 11:16
mvePete O'Hanlon31-Aug-16 11:16 
QuestionUWP. How to find out size of uwp Windows.Storage element? Pin
wind te26-Aug-16 22:42
Memberwind te26-Aug-16 22:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.