How to reference Button from xaml in ViewModel(c#) Xamarin - c#

I am trying to implement my own lockscreen in my xamarin app. I have created the numpad as below:
<ContentPage.Content>
<Grid Padding="0,0,0,10">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="2*" />
</Grid.RowDefinitions>
<Image Grid.Row="0" Source="alpaca.png" />
<Label Grid.Row="1" Text="{Binding PincodeMasked}" FontFamily="Lato-Bold" TextColor="Black" VerticalOptions="Center" FontSize="80" HorizontalTextAlignment="Center" />
<Grid Grid.Row="2" Padding="40,0,30,0">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Grid.Row="0" Grid.Column="0" Image="num1.png" Command="{Binding NumberCommand}" CommandParameter="1" BackgroundColor="#00FFFFFF" />
<Button Grid.Row="0" Grid.Column="1" Image="num2.png" Command="{Binding NumberCommand}" CommandParameter="2" BackgroundColor="#00FFFFFF" />
<Button Grid.Row="0" Grid.Column="2" Image="num3.png" Command="{Binding NumberCommand}" CommandParameter="3" BackgroundColor="#00FFFFFF" />
<Button Grid.Row="1" Grid.Column="0" Image="num4.png" Command="{Binding NumberCommand}" CommandParameter="4" BackgroundColor="#00FFFFFF" />
<Button Grid.Row="1" Grid.Column="1" Image="num5.png" Command="{Binding NumberCommand}" CommandParameter="5" BackgroundColor="#00FFFFFF" />
<Button Grid.Row="1" Grid.Column="2" Image="num6.png" Command="{Binding NumberCommand}" CommandParameter="6" BackgroundColor="#00FFFFFF" />
<Button Grid.Row="2" Grid.Column="0" Image="num7.png" Command="{Binding NumberCommand}" CommandParameter="7" BackgroundColor="#00FFFFFF" />
<Button Grid.Row="2" Grid.Column="1" Image="num8.png" Command="{Binding NumberCommand}" CommandParameter="8" BackgroundColor="#00FFFFFF" />
<Button Grid.Row="2" Grid.Column="2" Image="num9.png" Command="{Binding NumberCommand}" CommandParameter="9" BackgroundColor="#00FFFFFF" />
<Button Grid.Row="3" Grid.Column="1" Image="num0.png" Command="{Binding NumberCommand}" CommandParameter="0" BackgroundColor="#00FFFFFF" />
</Grid>
</Grid>
</ContentPage.Content>
In the ViewModel I want to do something like this
public PinLoginPageModel()
{
NumberCommand = new Command(NumpadToNumber);
}
void NumpadToNumber(object obj)
{
if(Button.CommandParameter == 0)
{
PincodeMasked = "0";
}
}
I want to convert each button click to a corresponding value, however I am getting an error at Button.CommandParameter saying it needs to have an object reference. How can I reference this to the button in the XAML code?
Thanks

You need to change your view mode code like below:
public PinLoginPageModel()
{
NumberCommand = new Command<string>(NumpadToNumber);
}
void NumpadToNumber(string obj)
{
if(obj == "0")
{
PincodeMasked = "0";
}
}

Try this:
void NumpadToNumber(object parameter)
{
if (parameter != null && parameter.ToString() == "0")
{
PincodeMasked = "0";
}
}

Just change your view model like this:
public PinLoginPageModel()
{
NumberCommand = new Command<object>(NumpadToNumber);
}
void NumpadToNumber(object obj)
{
if((obj as int) == 0)
PincodeMasked = "0";/
}

Related

WPF bind TextBlock "text" in one user control to textbox in another UserControl Where both of them are hosted in a Frame inside mainWindow

SUMMARY OF THE PROBLEM:
I have main window which has a frame and a button, in the frame i'm going to show one user control at a time (i provided an image of my usercontrol).
in the first UserControl, i will write 4 player names (two for each team).
after i press the button and i go to next usercontrol i want to show the names of the players in textblock. I also want to bind IsChecked property of radio button in first UserControl to the second UserControl. (i have other buttons that show up in mainWindow depends on what usercontrol is in the Frame right Now)
WHAT I TRIED:
I tried binding the textbox property to the textblock in the second usercontrol but it doesnt show or i cant access it.
because i have the button in the mainWindow i tried saving the names in variable then pass it to next usercontrol but didnt work also.
Also tried alot of random online solution i found online but nothing worked.
i need a way to expose the property of First UserControl to be used in the Second UserControl
MY CODE
MainWindow xaml
<Grid SnapsToDevicePixels="True" x:Name="myGrid" >
<Grid.Background>
<ImageBrush x:Name="myBackground" ImageSource="background1.png" Stretch="UniformToFill"/>
</Grid.Background>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="300" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="100" />
<RowDefinition Height="*" />
<RowDefinition Height=" auto" />
<RowDefinition Height=" 20" />
</Grid.RowDefinitions>
<Frame x:Name="Main" Grid.Row="0" Grid.RowSpan="2" Grid.Column="0" Grid.ColumnSpan="5" NavigationUIVisibility="Hidden" >
</Frame>
<Button Style="{StaticResource StandardButton}" Visibility="Visible" x:Name="playersBtn" Grid.Column="2" Grid.Row ="2" Margin="100,40,100,40" FontSize="36" Content="إبدأ" FontFamily="Ara Hamah Homs" Padding="50,0" Background="#FFFFE900" Click="playerInfoBtn" BorderBrush="{x:Null}" Foreground="Black" >
</Button>
MainWindow c#
namespace Try3
{
public partial class MainWindow : Window
{
SoundPlayer MP = new SoundPlayer();
public MainWindow()
{
InitializeComponent();
MP.SoundLocation = "C:/Users/eadh2/Desktop/game tries/try3/try3/sound1.wav";
MP.PlayLooping();
}
private void playerInfoBtn(object sender, RoutedEventArgs e)
{
Main.Content = new PlayersInfoPage();
playersBtn.Visibility = Visibility.Hidden;
startBtn.Visibility = Visibility.Visible;
icon.Visibility = Visibility.Collapsed;
ImageBrush b = new ImageBrush();
b.ImageSource = new BitmapImage(new Uri("pack://application:,,,/background flat.png"));
myGrid.Background = b;
}
private void startBtn_Click(object sender, RoutedEventArgs e)
{
Main.Content = new gamePage();
startBtn.Visibility = Visibility.Collapsed;
gamePageBoarder.Visibility = Visibility.Visible;
}
private void startBtn_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
startBtn.Background = Brushes.DarkGreen;
}
}
}
First usercontrol xaml to enter player names (Image of First UserControl to enter player names)
<UserControl x:Class="Try3.PlayersInfoPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Try3"
mc:Ignorable="d"
d:DesignHeight="862.179" d:DesignWidth="1210.632" >
<Grid SnapsToDevicePixels="True">
<Grid.ColumnDefinitions >
<ColumnDefinition Width="150" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width=" *" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="AUTO" />
<ColumnDefinition Width="150" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="100" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height=" *" />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<TextBlock Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="2" Text="بيانات الفريق الأحمر" FontSize="36" Margin="50,0,50,0" FontFamily="Ara Hamah Homs" />
<TextBox x:Name="redFirst" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" Margin="0,30,0,20" Padding="10" FontSize="36" HorizontalContentAlignment="Center" Text="اللاعب الأول" Foreground="#FF919191" GotMouseCapture="redFirst_GotMouseCapture" FontFamily="Ara Hamah Homs" />
<TextBox x:Name="redSecond" Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="2" Margin="0,30,0,110" Padding="10" FontSize="36" HorizontalContentAlignment="Center" Text="اللاعب الثاني" Foreground="#FF919191" GotMouseCapture="redSecond_GotMouseCapture1" FontFamily="Ara Hamah Homs" />
<TextBlock Grid.Column="4" Grid.Row="1" Grid.ColumnSpan="2" Text="بيانات الفريق الأخضر " FontSize="36" Margin="50,0,50,20" FontFamily="Ara Hamah Homs" />
<TextBox x:Name="greenFirst" Grid.Row="2" Grid.Column="4" Grid.ColumnSpan="2" Margin="0,30,0,20" Padding="10" FontSize="36" HorizontalContentAlignment="Center" Text="اللاعب الأول" Foreground="#FF919191" GotMouseCapture="greenFirst_GotMouseCapture2" FontFamily="Ara Hamah Homs" />
<TextBox x:Name="greenSecond" Grid.Row="3" Grid.Column="4" Grid.ColumnSpan="2" Margin="0,30,0,110" Padding="10" FontSize="36" HorizontalContentAlignment="Center" Text="اللاعب الثاني" Foreground="#FF919191" GotMouseCapture="greenSecond_GotMouseCapture3" FontFamily="Ara Hamah Homs" />
<Border x:Name="roundsBoarder" Grid.Column="3" Grid.Row="2" Grid.RowSpan="2" BorderThickness ="3" BorderBrush="Black" CornerRadius="0" Margin="20,50,20,130" MaxHeight="170" MaxWidth="350" VerticalAlignment="Center" Background="#99FFFFFF" MouseEnter="Border_MouseEnter" MouseLeave="roundsBoarder_MouseLeave">
<StackPanel HorizontalAlignment="Center" >
<StackPanel Orientation="Horizontal" >
<Viewbox Height="40" Width="40" >
<RadioButton BorderBrush="#FFFFE900" VerticalContentAlignment="Center" HorizontalAlignment="Left" GroupName="roundsNumber" FontFamily="Ara Hamah Homs" FontSize="12"/>
</Viewbox>
<TextBlock Text="جولة" FontSize="42" Margin="20,0,0,0" FontFamily="Alnaqaaa R" />
</StackPanel>
<StackPanel Orientation="Horizontal" >
<Viewbox Height="40" Width="40" >
<RadioButton BorderBrush="#FFFFE900" VerticalContentAlignment="Center" HorizontalAlignment="Left" GroupName="roundsNumber" FontFamily="Ara Hamah Homs" FontSize="12"/>
</Viewbox>
<TextBlock Text="ثلاث جولات" FontSize="42" Margin="20,0,0,0" FontFamily="Alnaqaaa R" />
</StackPanel>
</StackPanel>
</Border>
</Grid>
First UserControl C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Navigation;
using System.Windows.Shapes;
namespace Try3
{
public partial class PlayersInfoPage : UserControl
{
public PlayersInfoPage()
{
InitializeComponent();
}
private void redFirst_GotMouseCapture(object sender, MouseEventArgs e)
{
redFirst.Text = "";
redFirst.Foreground = Brushes.Black;
}
private void redSecond_GotMouseCapture1(object sender, MouseEventArgs e)
{
redSecond.Text = "";
redSecond.Foreground = Brushes.Black;
}
private void greenFirst_GotMouseCapture2(object sender, MouseEventArgs e)
{
greenFirst.Text = "";
greenFirst.Foreground = Brushes.Black;
}
private void greenSecond_GotMouseCapture3(object sender, MouseEventArgs e)
{
greenSecond.Text = "";
greenSecond.Foreground = Brushes.Black;
}
private void Save_MouseEnter(object sender, MouseEventArgs e)
{
}
private void Save_MouseLeave(object sender, MouseEventArgs e)
{
}
private void Border_MouseEnter(object sender, MouseEventArgs e)
{
roundsBoarder.Background = new SolidColorBrush(Colors.White) { Opacity = 1 };
roundsBoarder.CornerRadius = new CornerRadius(20);
}
private void roundsBoarder_MouseLeave(object sender, MouseEventArgs e)
{
roundsBoarder.Background = new SolidColorBrush(Colors.White) { Opacity = 0.6 };
roundsBoarder.CornerRadius = new CornerRadius(0);
}
}
}
Second UserControl xaml to show player names in each side
<UserControl x:Class="Try3.gamePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:try3"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Grid.ColumnDefinitions >
<ColumnDefinition Width="50" />
<ColumnDefinition Width="60" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="60" />
<ColumnDefinition Width="50" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions >
<RowDefinition Height="50" />
<RowDefinition Height="*" />
<RowDefinition Height="auto" />
<RowDefinition Height="50" />
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<StackPanel Orientation="Vertical" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1" >
<TextBlock HorizontalAlignment="Center" FontSize="36" FontFamily="Ara Hamah Homs" Text="الفريق الأحمر" />
<TextBlock x:Name="redFirstPlayer" FontSize="24" FontFamily="Ara Hamah Homs" Text="اللاعب الاول" HorizontalAlignment="Center"/>
<TextBlock x:Name="redSecondPlayer" FontSize="24" FontFamily="Ara Hamah Homs" Text="اللاعب الثاني" HorizontalAlignment="Center"/>
</StackPanel>
<StackPanel Orientation="Vertical" Grid.Column="6" Grid.ColumnSpan="2" Grid.Row="1">
<TextBlock FontSize="36" FontFamily="Ara Hamah Homs" Text="الفريق الأخضر" HorizontalAlignment="Center" Margin="0,0,0,20"/>
<TextBlock x:Name="greenFirstPlayer" FontSize="24" FontFamily="Ara Hamah Homs" Text="اللاعب الاول" HorizontalAlignment="Center"/>
<TextBlock x:Name="greenSecondPlayer" FontSize="24" FontFamily="Ara Hamah Homs" Text="اللاعب الثاني" HorizontalAlignment="Center"/>
</StackPanel>
<TextBlock x:Name="redScore" Grid.Column="1" Grid.Row="3" FontSize="42" FontFamily="Ara Hamah Homs" Text="0" HorizontalAlignment="center" />
<Image Source="trophy (1).png" Grid.Column="2" Grid.Row="3" HorizontalAlignment="Left"/>
<TextBlock x:Name="greenScore" Grid.Column="7" Grid.Row="3" FontSize="42" FontFamily="Ara Hamah Homs" HorizontalAlignment="center" Text="0" />
<Image Source="trophy (1).png" Grid.Column="6" Grid.Row="3" HorizontalAlignment="Right"/>
<StackPanel Grid.Row="1" Grid.Column="6" Grid.ColumnSpan="2" Margin="0,50,0,0" >
</StackPanel>
</Grid>

WPF sliders to change button colors on click

I am doing a project where I have to change button colors based on sliders in WPF. This is my XAML design.
XAML file
So, I have made 64 buttons, and I have tried implementing multiple options, and none worked for me.
I have this xaml code:
<Grid Grid.Column="0" Grid.RowSpan="4" Grid.ColumnSpan="4" HorizontalAlignment="Left" Height="790" Margin="0,10,0,0" Grid.Row="0" VerticalAlignment="Top" Width="800">
<Grid Margin="230,114,230,346" AutomationProperties.Name="ledMatrix" Tag="ledMatrix" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Button Grid.Column="0" Grid.Row="0" Height="40" Width="40" Command="{Binding LEDBtn}" Background="#FFAAAAAA" Tag="LED00" Click="changeLedIndicatiorColor" x:Name="LEDButton"/>
<Button Grid.Column="1" Grid.Row="0" Height="40" Width="40" Command="{Binding LEDBtn}" Background="#FFAAAAAA" Tag="LED01"/>
<Button Grid.Column="2" Grid.Row="0" Height="40" Width="40" Command="{Binding LEDBtn}" Background="#FFAAAAAA" Tag="LED02"/>
<Button Grid.Column="3" Grid.Row="0" Height="40" Width="40" Command="{Binding LEDBtn}" Background="#FFAAAAAA" Tag="LED03"/>
<Button Grid.Column="4" Grid.Row="0" Height="40" Width="40" Command="{Binding LEDBtn}" Background="#FFAAAAAA" Tag="LED04"/>
<Button Grid.Column="5" Grid.Row="0" Height="40" Width="40" Command="{Binding LEDBtn}" Background="#FFAAAAAA" Tag="LED05"/>
<Button Grid.Column="6" Grid.Row="0" Height="40" Width="40" Command="{Binding LEDBtn}" Background="#FFAAAAAA" Tag="LED06"/>
<Button Grid.Column="7" Grid.Row="0" Height="40" Width="40" Command="{Binding LEDBtn}" Background="#FFAAAAAA" Tag="LED07"/>
and sliders:
<Grid Margin="230,480,230,170">
<Slider x:Name="SeekR" Margin="0,0,0,120" Tag="seekR" Maximum="255" Background="Red" ></Slider>
<Slider x:Name="SeekG" Margin="0,50,0,70" Tag="seekG" Maximum="255" Background="Green"></Slider>
<Slider x:Name="SeekB" Margin="0,100,0,20" Tag="seekB" Maximum="255" Background="Blue"></Slider>
</Grid>
I have tried placing this in my xaml.cs file :
public void changeColor()
{
byte rr = (byte)SeekR.Value;
byte gg = (byte)SeekG.Value;
byte bb = (byte)SeekB.Value;
Color cc = Color.FromRgb(rr, gg, bb); //Create object of Color class.
SolidColorBrush colorBrush = new SolidColorBrush(cc); //Creating object of SolidColorBruch class.
LEDButton.Background = colorBrush; //Setting background of stack panel.
}
private void changeLedIndicatiorColor(object sender, RoutedEventArgs e)
{
changeColor();
}
But from this code, I have to name all the buttons differently butI want to have the same binding for all buttons and try to connect it all together, so for example I would like to set color by sliders and when I click the button color would change. Then, I can change the color, and make other buttons that different color.
Refactor ChangeColor to accept a Button, the button to change the color for:
private void ChangeLedIndicatiorColor_OnClick(object sender, RoutedEventArgs e)
{
ChangeColor(sender as Button);
}
public void ChangeColor(Button button)
{
byte rr = (byte)SeekR.Value;
byte gg = (byte)SeekG.Value;
byte bb = (byte)SeekB.Value;
Color cc = Color.FromRgb(rr, gg, bb);
SolidColorBrush colorBrush = new SolidColorBrush(cc);
button.Background = colorBrush;
}
Then register the ChangeLedIndicatiorColor event handler for each Button.Click:
...
<Button Click="ChangeLedIndicatiorColor" />
<Button Click="ChangeLedIndicatiorColor" />
<Button Click="ChangeLedIndicatiorColor" />
...
You can use the EventSetter so that the event can be applicable to all the buttons. You don't need to attach the click event handler to each button anymore.
<Window.Resources>
<Style TargetType="Button">
<EventSetter Event="Click" Handler="ChangeLedIndicatiorColor"/>
</Style>
</Window.Resources>
And inside event handler you can change the color of the button which triggered the click event.
private void changeLedIndicatiorColor(object sender, RoutedEventArgs e)
{
byte rr = (byte)SeekR.Value;
byte gg = (byte)SeekG.Value;
byte bb = (byte)SeekB.Value;
Color cc = Color.FromRgb(rr, gg, bb);
SolidColorBrush colorBrush = new SolidColorBrush(cc);
(sender as Button).Background = colorBrush;
}

Data Binding Xamarin Forms

Im quite new to xaml and xamarin.forms and I'm having some trouble with Data Bindings. I know there is a lot of similar questions out there but nothing I've found seem to work. My actual goal is to change background color when I turn a switch on and off. But for now I just want to make the binding work so I'm using a string in the example. My Page.xaml.cs looks like this
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CounterMainPage : ContentPage
{
public CounterMainPage ()
{
InitializeComponent ();
BindingContext = new CounterMainPageViewModel();
}
My viewmodel looks like this:
class CounterMainPageViewModel
{
public Color BackGroundColor = Color.Red;
public string Testning = "Urgent";
}
My Converter class looks like this:
public class BoolToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var valueAsString = value.ToString();
switch (valueAsString)
{
case ("Urgent"):
{
return Color.FromHex("#EF3D60");
}
case ("Not urgent"):
{
return Color.FromHex("#231F20");
}
default:
{
return Color.FromHex("#231F20");
}
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
And last but not least my page.xaml looks like this:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:c="clr-namespace:BlankClient"
xmlns:local="clr-namespace:BlankClient"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="BlankClient.View.CounterMainPage">
<!--Style="{StaticResource PageStyle}">-->
<ContentPage.Resources>
<ResourceDictionary>
<c:BoolToColorConverter x:Key="colorConverter"></c:BoolToColorConverter>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="50" />
<RowDefinition Height="50" />
<RowDefinition Height="50" />
<RowDefinition Height="50" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label x:Name="ReceivedFromServer" Text="" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3"/>
<Button Text="A" StyleId="0" Clicked="Button_Clicked" Grid.Row="1" Grid.Column="0"/>
<Button Text="2" StyleId="1" Clicked="Button_Clicked" Grid.Row="1" Grid.Column="1" />
<Button Text="3" StyleId="2" Clicked="Button_Clicked" Grid.Row="1" Grid.Column="2"/>
<Button Text="4" StyleId="3" Clicked="Button_Clicked" Grid.Row="2" Grid.Column="0"/>
<Button Text="5" StyleId="4" Clicked="Button_Clicked" Grid.Row="2" Grid.Column="1"/>
<Button Text="6" StyleId="5" Clicked="Button_Clicked" Grid.Row="2" Grid.Column="2"/>
<Button Text="7" StyleId="6" Clicked="Button_Clicked" Grid.Row="3" Grid.Column="0"/>
<Button Text="8" StyleId="7" Clicked="Button_Clicked" Grid.Row="3" Grid.Column="1"/>
<Button Text="9" StyleId="8" Clicked="Button_Clicked" Grid.Row="3" Grid.Column="2"/>
<Button Text="10" StyleId="9" Clicked="Button_Clicked" Grid.Row="4" Grid.Column="0" BackgroundColor="{Binding Testning,Converter={StaticResource colorConverter}}"/>
<Button Text="Undo" StyleId="Undo" Clicked="Button_Clicked" Grid.Row="4" Grid.Column="1" IsEnabled="False" />
<Button Text="Redo" StyleId="Undo " Clicked="Button_Clicked" Grid.Row="4" Grid.Column="2" IsEnabled="False"/>
<Switch Grid.Row="5" Grid.Column="0" />
</Grid>
<!--<StackLayout>
<Label Text="Counter Main Page" />
<Button Text="5" StyleId="5" Clicked="Button_Clicked"/>
<Button Text="10" StyleId="10" />
<Label x:Name="ReceivedFromServer" Text="ewq"/>
</StackLayout>-->
</ContentPage.Content>
First of all "public string Testning = "Urgent";" should be property to bind it to view. Read output and you will see that there is exception that "Testning" property is not found.
Second: you need to implement INotifyPropertyChanged interface (or use framework like Prism) to be able to change property value and inform UI about that change

How to assign List<> Object values to textBox (s)

I want to assign the values of the findFamily object to the different TextBox(s) i tried the following code but it's not working would you please suggest me a better way. Thanks in advance.
private void Search_Click(object sender, RoutedEventArgs e)
{
if (SearchFamilyMemberId.Text.Trim() != "")
{
SITDataDataContext con = new SITDataDataContext();
List<Family> findFamily = (from s in con.Families where s.FamilyMemberId.Equals(SearchFamilyMemberId.Text.Trim()) select s).ToList();
if (findFamily.Any())
{
FamilyMemberId.Text = findFamily[0];
FirstName.Text = findFamily[1];
LastName.Text = findFamily[2];
if (findFamily[3]=="Male")
{
Male.IsChecked = true;
}
else
{
Female.IsChecked = true;
}
Phone.Text = findFamily[4];
Address.Text = findFamily[5];
}
else
{
MessageBox.Show("Family Id not found!");
}
}
else
{
MessageBox.Show("Invalid Id!");
}
}
here is my xamal code
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="1" Grid.Column="0" Content="Family Member ID:"/>
<Label Grid.Row="2" Grid.Column="0" Content="First Name:"/>
<Label Grid.Row="3" Grid.Column="0" Content="Last Name:"/>
<Label Grid.Row="4" Grid.Column="0" Content="Gender:"/>
<Label Grid.Row="5" Grid.Column="0" Content="Phone:"/>
<Label Grid.Row="6" Grid.Column="0" Content="Address:"/>
<CheckBox Name="ColonyResident" Content="Colony Resident" Grid.Row="0" Grid.Column="0" Margin="5" Checked="ColonyResident_Checked" Unchecked="ColonyResident_Unchecked"/>
<TextBox Name="SearchFamilyMemberId" IsEnabled="False" SelectionChanged="FamilyMemberId_SelectionChanged" Grid.Row="0" Grid.Column="1" Margin="3"/>
<TextBox Name="FamilyMemberId" IsEnabled="False" Grid.Row="1" Grid.Column="1" Margin="3" Grid.ColumnSpan="2"/>
<TextBox Name="FirstName" Grid.Row="2" Grid.Column="1" Margin="3" Grid.ColumnSpan="2"/>
<TextBox Name="LastName" Grid.Row="3" Grid.Column="1" Margin="3" Grid.ColumnSpan="2"/>
<RadioButton Name="Male" Grid.Row="4" Grid.Column="1" Margin="3" Content="Male" />
<RadioButton Name="Female" Grid.Row="4" Grid.Column="2" Margin="3" Content="Female" />
<TextBox Name="Phone" Grid.Row="5" Grid.Column="1" Margin="3" Grid.ColumnSpan="2"/>
<TextBox Name="Address" Grid.Row="6" Grid.Column="1" Margin="3" Grid.ColumnSpan="2" TextWrapping="Wrap"/>
<Button Name="Search" IsEnabled="False" Grid.Row="0" Grid.Column="2" Margin="3" MinWidth="100" MinHeight="25" HorizontalAlignment="Center" Content="Search" Click="Search_Click"/>
<Button IsDefault="True" Grid.Row="7" Grid.Column="1" Margin="3" MinWidth="100" MinHeight="25" HorizontalAlignment="Center" Content="Add" Click="Add_Click"/>
<Button Grid.Row="7" Grid.Column="2" Margin="3" MinWidth="100" MinHeight="25" HorizontalAlignment="Center" Content="Clear" Click="Clear_Click"/>
</Grid>
Why don't you use data binding?
For each of the text boxes that you have, bind your TextBox.Text to its corresponding property in the view model, something like this:
<TextBox Text={Binding FirstName} />
Then, assign your FirstName, LastName...etc. properties to values and raise INotifyPropertyChanged.PropertyChanged for each of the properties (your view model should implement INotifyPropertyChanged) and your view (i.e. UI) should get updated.
I don't agree with any of the answers posted here. The question doesn't seem to be correct.
If you have a list of Family members. Then how can you show it as a single item. How do you know which item you want to display.
Hence my suggestion is to use a listbox and using data binding, create a view to display the current selected Family member's data in that view.
use foreach loop through list as:
foreach(var a in findFamily)
{
txtbox.text=a[0].tostring();
txtbox2.text=a[1].tostring();
}
or use:
txtbox.text = findFamily.Select(a => a[0].ToString()).FirstOrDefault().ToString();

Silverlight - Adding MediaElement via C# - short time to manage Play() method

I made simple app in Windows Phone Silverlight 8.1.
It looks like cell phone keypad with lcd-looks like screen that shows number of current tapped button and plays tone sound of that number.
I've been searching for a while but i found nothing specific.
I Have problem with Play(); method because it does execute while i'm debugging but in realtime after app-deploy to my device i hear nothing...
I researched many topics and I can tell that it depends on
mediaElementObject.CurrentState;
Sometimes it's on "Closed" state and sometimes on "Opening"(and then i hear the sound while debugging).
C#
private void KeypadButtonClick(object sender, RoutedEventArgs e)
{
Button currentButton = sender as Button;
MediaElement keySound = null;
if (currentButton != null)
{
string buttonContentValue = currentButton.Content.ToString();
keySound = new MediaElement();
ContentPanel.Children.Add(keySound);
PlayTargetKeypadSound(buttonContentValue, keySound);
RefreshNumber(buttonContentValue);
}
if (keySound != null)
{
ContentPanel.Children.Remove(keySound);
}
}
private void PlayTargetKeypadSound(string buttonContentValue, MediaElement keySound)
{
String path = "/Assets/Sounds/keyNumber_" + buttonContentValue + ".wav";
keySound.CurrentStateChanged += KeySoundOnCurrentStateChanged;
keySound.Source = new Uri(path, UriKind.RelativeOrAbsolute);
keySound.Volume = 1.0;
keySound.AutoPlay = false;
}
private void KeySoundOnCurrentStateChanged(object sender, RoutedEventArgs routedEventArgs)
{
MediaElement sound = sender as MediaElement;
if (sound != null) sound.Play();
}
private void RefreshNumber(string buttonContentValue)
{
NumberTextBlock.Text = buttonContentValue;
}
XAML
<Grid x:Name="ContentPanel" Grid.Row="2" Margin="8,0,8,12">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- First Row -->
<Button Click="KeypadButtonClick" Content="1" Style="{StaticResource KeypadButtonStyle}" />
<Button Click="KeypadButtonClick" Content="2" Style="{StaticResource KeypadButtonStyle}" Grid.Column="1" />
<Button Click="KeypadButtonClick" Content="3" Style="{StaticResource KeypadButtonStyle}" Grid.Column="2" />
<!-- Second Row -->
<Button Click="KeypadButtonClick" Content="4" Style="{StaticResource KeypadButtonStyle}" Grid.Row="1" />
<Button Click="KeypadButtonClick" Content="5" Style="{StaticResource KeypadButtonStyle}" Grid.Row="1" Grid.Column="1" />
<Button Click="KeypadButtonClick" Content="6" Style="{StaticResource KeypadButtonStyle}" Grid.Row="1" Grid.Column="2" />
<!-- Third Row -->
<Button Click="KeypadButtonClick" Content="7" Style="{StaticResource KeypadButtonStyle}" Grid.Row="2" />
<Button Click="KeypadButtonClick" Content="8" Style="{StaticResource KeypadButtonStyle}" Grid.Row="2" Grid.Column="1" />
<Button Click="KeypadButtonClick" Content="9" Style="{StaticResource KeypadButtonStyle}" Grid.Row="2" Grid.Column="2" />
<!-- Fourth Row -->
<Button Click="KeypadButtonClick" Content="*" Style="{StaticResource KeypadButtonStyle}" Grid.Row="3" />
<Button Click="KeypadButtonClick" Content="0" Style="{StaticResource KeypadButtonStyle}" Grid.Row="3" Grid.Column="1" />
<Button Click="KeypadButtonClick" Content="#" Style="{StaticResource KeypadButtonStyle}" Grid.Row="3" Grid.Column="2" />
</Grid>
Many thanks for helping
Ok.
I solved it...
C#
private void KeypadButtonClick(object sender, RoutedEventArgs e)
{
Button currentButton = sender as Button;
if (currentButton != null)
{
string buttonContentValue = currentButton.Content.ToString();
PlayTargetKeypadSound(buttonContentValue);
RefreshNumber(buttonContentValue);
}
}
private void PlayTargetKeypadSound(string buttonContentValue)
{
String path = "/Assets/Sounds/keyNumber_" + buttonContentValue + ".wav";
KeySoundMediaElement.MediaOpened += KeySoundMediaElementOnMediaOpened;
KeySoundMediaElement.Source = new Uri(path, UriKind.RelativeOrAbsolute);
}
private void KeySoundMediaElementOnMediaOpened(object sender, RoutedEventArgs routedEventArgs)
{
MediaElement sound = sender as MediaElement;
if (sound != null) sound.Play();
}
private void RefreshNumber(string buttonContentValue)
{
NumberTextBlock.Text = buttonContentValue;
}
XAML
<!-- Fourth Row -->
<Button Click="KeypadButtonClick" Content="*" Style="{StaticResource KeypadButtonStyle}" Grid.Row="3" />
<Button Click="KeypadButtonClick" Content="0" Style="{StaticResource KeypadButtonStyle}" Grid.Row="3" Grid.Column="1" />
<Button Click="KeypadButtonClick" Content="#" Style="{StaticResource KeypadButtonStyle}" Grid.Row="3" Grid.Column="2" />
<MediaElement
x:Name="KeySoundMediaElement"
Volume="1"
AutoPlay="False"
/>

Categories