Set selected item in WPF ComboBox [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
(I've read similar posts but they all had a twist to them that made the solution different)
I'm porting a WinForms app that used this:
myComboBox.SetSelected(myComboBox.FindString("Some Text"), true);
to select an item programmatically. When porting over to WPF, I tried this but it has no effect (the item does not get selected):
myComboBox.SelectedItem = myComboBox.FindName("Some Text");
What is the correct way to select an existing item in a ComboBox, in WPF?

You have to use SelectedValue. In WPF ComboBox, there are multiple ways to achieve the same thing. So, one syntax to select an item programmatically won't work. There are various ways of adding items to ComboBox.
You can set ItemsSource both declaratively or in code.
You can add ComboBoxItems etc. See Items property in property window to see various item-types available.
If you are using ItemsSource with string values, then you need syntax like : cmb1.SelectedValue = "Name1"
If you are directly adding items like <ComboBox ...> <ComboBoxItem Content="Name1"/> </ComboBox/> , then you need
foreach (ComboBoxItem item in cmb2.Items)
if (item.Content.ToString() == "Name1")
{
cmb2.SelectedValue = item;
break;
}
I have posted a full working sample demonstrating how to select an item programmatically in various scenarios. Sample code (can be used as is) :
Pay attention to last one, where you have to use SelectedValuePath.
Window1.xaml
<Window x:Class="WpfApplicationBlend.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="Window1" Height="411" Width="749">
<Grid>
<Grid Margin="30,27,491,276">
<ComboBox x:Name="cmb1" HorizontalAlignment="Left" Margin="0,28,0,0" VerticalAlignment="Top" Width="210" Height="25" FocusVisualStyle="{DynamicResource StyleFocusDefault}">
<ComboBox.ItemsSource>
<CompositeCollection>
<sys:String>Name1</sys:String>
<sys:String>Name2</sys:String>
<sys:String>Name3</sys:String>
<sys:String>Name4</sys:String>
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
<TextBox x:Name="tbInput1" HorizontalAlignment="Left" Height="23" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="135,1,0,0" VerticalAlignment="Top" Width="75" Click="Button1_Click"/>
</Grid>
<Grid Margin="405,27,111,276">
<ComboBox x:Name="cmb2" HorizontalAlignment="Left" Margin="0,28,0,0" VerticalAlignment="Top" Width="210" Height="25" FocusVisualStyle="{DynamicResource StyleFocusDefault}">
<ComboBoxItem Content="Name1"/>
<ComboBoxItem Content="Name2"/>
<ComboBoxItem Content="Name3"/>
</ComboBox>
<TextBox x:Name="tbInput2" HorizontalAlignment="Left" Height="23" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="135,1,0,0" VerticalAlignment="Top" Width="75" Click="Button2_Click"/>
</Grid>
<Grid Margin="30,207,491,96">
<ComboBox x:Name="cmb3" HorizontalAlignment="Left" Margin="0,28,0,0" VerticalAlignment="Top" Width="210" Height="25" FocusVisualStyle="{DynamicResource StyleFocusDefault}">
<ComboBox.ItemsSource>
<CompositeCollection>
<sys:String>Name1</sys:String>
<sys:Boolean>True</sys:Boolean>
<sys:Int32>123</sys:Int32>
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
<TextBox x:Name="tbInput3" HorizontalAlignment="Left" Height="23" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="135,1,0,0" VerticalAlignment="Top" Width="75" Click="Button3_Click"/>
</Grid>
<Grid Margin="405,207,116,96">
<ComboBox x:Name="cmb4" HorizontalAlignment="Left" Margin="0,28,0,0" VerticalAlignment="Top" Width="210" Height="25" SelectedValuePath="Name" DisplayMemberPath="Name">
</ComboBox>
<TextBox x:Name="tbInput4" HorizontalAlignment="Left" Height="23" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="135,1,0,0" VerticalAlignment="Top" Width="75" Click="Button4_Click"/>
</Grid>
</Grid>
</Window>
Window1.xaml.cs
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Collections;
namespace WpfApplicationBlend
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
List<Employee> employees = new List<Employee>()
{
new Employee(){Name="Name1", Age=100},
new Employee(){Name="Name2", Age=101},
};
cmb4.ItemsSource = employees;
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
cmb1.SelectedValue = tbInput1.Text;
}
private void Button2_Click(object sender, RoutedEventArgs e)
{
foreach (ComboBoxItem item in cmb2.Items)
if (item.Content.ToString() == tbInput2.Text)
{
cmb2.SelectedValue = item;
break;
}
}
private void Button3_Click(object sender, RoutedEventArgs e)
{
foreach (object item in cmb3.Items)
if (item.ToString() == tbInput3.Text)
{
cmb3.SelectedValue = item;
break;
}
}
private void Button4_Click(object sender, RoutedEventArgs e)
{
cmb4.SelectedValue = tbInput4.Text;
}
}
public class Employee
{
public string Name { get; set; }
public int Age { get; set; }
}
}

comboboxName.SelectedIndex = yourIndex;
e.g.
combobox1.SelectedIndex = 2;

Related

Need to get my enum inserted into a combobox in C#

I need help with getting the information in my enum into a combobox.
Here is the code for my enum:
namespace Arsalan_Salam_991571527_A2
{
public enum CarType
{
Odyssey,
Rouge,
Sienna,
Accord
}
}
I have found some code that was suppose to work and I tried to implement into my code to make the information inside of enum appear as shown below:
private void AddingEnumIntoComboBox(Car c)
{
foreach (var item in Enum.GetValues(typeof(CarType)))
{
carTypeInput.Items.Add(item);
}
}
But for some reason the program works fine but this code does not show the information of my enum into the combobox which is called carTypeInput. This is for a college assignment.
Here is the xaml that I used to create the UI interface:
<Page
x:Class="Arsalan_Salam_991571527_A2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Arsalan_Salam_991571527_A2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid RenderTransformOrigin="0.497,0.522">
<TextBlock HorizontalAlignment="Left" Margin="572,10,0,0" Text="DriveWell Inc." TextAlignment="Center" FontSize="50" VerticalAlignment="Top" Width="374" Height="72"/>
<TextBlock HorizontalAlignment="Left" Margin="87,88,0,0" Text="Vin Number" TextAlignment="Center" FontSize="30" VerticalAlignment="Top" Width="192" Height="67" RenderTransformOrigin="0.457,-0.751"/>
<TextBlock HorizontalAlignment="Left" Margin="67,185,0,0" Text="Car Make" TextAlignment="Center" FontSize="30" VerticalAlignment="Top" Width="194" Height="63"/>
<TextBlock HorizontalAlignment="Left" Margin="87,282,0,0" Text="Car Type" TextAlignment="Center" FontSize="30" VerticalAlignment="Top" Width="183" Height="61"/>
<TextBlock HorizontalAlignment="Left" Text="Purchase Price" TextAlignment="Center" FontSize="30" VerticalAlignment="Top" Margin="87,380,0,0" Width="226" Height="61" RenderTransformOrigin="3.948,-0.233"/>
<TextBlock HorizontalAlignment="Left" Margin="87,487,0,0" Text="Model Year" TextAlignment="Center" FontSize="30" VerticalAlignment="Top" Height="65" Width="190" RenderTransformOrigin="3.283,-2.555"/>
<TextBlock HorizontalAlignment="Left" Margin="87,584,0,0" Text="Mileage (Km)" TextAlignment="Center" FontSize="30" VerticalAlignment="Top" Height="43" Width="192"/>
<Button x:Name="addingCar" Click="addingCar_Click" Content="Add Car" FontSize="30" Margin="43,639,0,0" VerticalAlignment="Top" Height="56" Width="156"/>
<Button x:Name="clearing" Click="clearing_Click" Content="Clear" FontSize="30" Margin="224,639,0,0" VerticalAlignment="Top" Height="56" Width="134"/>
<Button x:Name="updatingCar" Click="updatingCar_Click" Content="Update" FontSize="30" Margin="379,639,0,0" VerticalAlignment="Top" Height="56" Width="130"/>
<ComboBox x:Name="carTypeInput" Margin="348,282,0,0" Width="191" Height="57"/>
<ComboBox x:Name="modelYearInput" Margin="348,483,0,0" Width="191" Height="52"/>
<TextBox x:Name="vinNumberInput" HorizontalAlignment="Left" Margin="348,88,0,0" Text="" FontSize="25" VerticalAlignment="Top" Height="40" Width="191" RenderTransformOrigin="0.476,-1.383"/>
<TextBox x:Name="carMakeInput" HorizontalAlignment="Left" Margin="348,185,0,0" Text="" FontSize="25" VerticalAlignment="Top" Height="40" Width="191"/>
<TextBox x:Name="purchasePriceInput" HorizontalAlignment="Left" Margin="348,380,0,0" Text="" FontSize="25" VerticalAlignment="Top" Height="52" Width="191"/>
<TextBox x:Name="mileageInput" HorizontalAlignment="Left" Margin="348,584,0,0" Text="" FontSize="15" VerticalAlignment="Top" Height="32" Width="191"/>
<Image x:Name="carImageOutput" HorizontalAlignment="Left" Height="429" Margin="1013,106,0,0" VerticalAlignment="Top" Width="226"/>
<TextBlock x:Name="errorMessageOutput" HorizontalAlignment="Left" Margin="572,624,0,0" Text="" FontSize="35" VerticalAlignment="Top" Width="641" Height="62"/>
<ListView x:Name="lstCarDetailOutput" Margin="572,88,315,120"></ListView>
</Grid>
</Page>
jdweng pointed this out in their comment, but I'll expand on it and make it an answer:
The problem is that Enum.GetValues returns the values of an enum, which is a integral type (currently C# enums are more or less a fancy wrapper around a bunch of constant numbers). By default this is a int (more here). Meaning your call to Enum.GetValues(typeof(CarType)) is returning int[]. Now there are multiple ways to get the name of an enum value, I'll demonstrate two.
Convert the int back to an enum value and call ToString
foreach (var item in Enum.GetValues(typeof(CarType))
{
// This can be written as 'carTypeInput.Items.Add(((CarType) item).ToString());'
var carType = (CarType) item;
carTypeInput.Items.Add(carType.ToString());
}
Use Enum.GetName to avoid having to get an instance of CarType
foreach (var item in Enum.GetValue(typeof(CarType))
{
// This can be written as 'carTypeInput.Items.Add(Enum.GetName(typeof(CarType), item));
var carTypeName = Enum.GetName(typeof(CarType), item);
carTypeInput.Items.Add(carTypeName);
}
Why not use Enum.GetNames?
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.AddRange(Enum.GetNames(typeof(CarType)));
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string carTypeName = comboBox1.SelectedItem.ToString();
if(carTypeName == CarType.Accord.ToString())
{
...
}
}

delayed trigger for calculation with data binding

I'm very new to WPF and currently learning the concepts of data binding.
my simplified XAML code. besides my problem (below) it works fine - quick and dirty placing of objects via GUI, will be cleaned up once works:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition/>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
</Grid>
<Grid Grid.Row="1">
<GroupBox Header="Change Type:" Height="95" Width="100" VerticalAlignment="Top" Margin="270,4,422,0" >
<StackPanel>
<RadioButton x:Name="RbtAdd" HorizontalAlignment="Left" Margin="5" VerticalAlignment="Top" GroupName="modGroup" Checked="ModeRadio_Checked">
<WrapPanel>
<TextBlock Text="Add" Foreground="Green"/>
</WrapPanel>
</RadioButton>
<RadioButton x:Name="RbtPull" HorizontalAlignment="Left" Margin="5" VerticalAlignment="Top" GroupName="modGroup" Checked="ModeRadio_Checked">
<WrapPanel>
<TextBlock Text="Pull" Foreground="Blue"/>
</WrapPanel>
</RadioButton>
<RadioButton x:Name="RbtModify" HorizontalAlignment="Left" Margin="5" VerticalAlignment="Top" GroupName="modGroup" Checked="ModeRadio_Checked">
<WrapPanel>
<TextBlock Text="Modify" Foreground="DarkGray"/>
</WrapPanel>
</RadioButton>
</StackPanel>
</GroupBox>
<TextBlock x:Name="txtCurStock" HorizontalAlignment="Left" Margin="330,181,0,0" TextWrapping="Wrap" Text="{Binding Path=CurrentStock}" VerticalAlignment="Top" FontSize="20" FontWeight="Bold" TextAlignment="Center"/>
<Label Content="Current stock:" HorizontalAlignment="Left" Margin="289,156,0,0" VerticalAlignment="Top"/>
<Label x:Name ="lblOperation" Content="Stock to Pull:" HorizontalAlignment="Left" Margin="507,156,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtEntry" HorizontalAlignment="Left" Height="32" Margin="489,181,0,0" TextWrapping="Wrap" TextAlignment="Center" Text="{Binding Path=ModEntry}" VerticalAlignment="Top" Width="120" FontSize="20" FontWeight="Bold" TextChanged="TxtEntry_TextChanged"/>
<Label Content="New Stock" HorizontalAlignment="Right" Margin="714,156,0,0" VerticalAlignment="Top" Width="68"/>
<TextBlock Text="{Binding Path=NewStock}" HorizontalAlignment="Right" Margin="0,186,10,0" TextAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="20" FontWeight="Bold" Width="68"/>
<TextBox x:Name="txtComment" HorizontalAlignment="Left" Height="86" Margin="289,233,0,0" TextWrapping="Wrap" Text="{Binding Path=ModEntry}" VerticalAlignment="Top" Width="493"/>
<Label Content="Comment:" HorizontalAlignment="Left" Margin="289,207,0,0" VerticalAlignment="Top"/>
<TextBlock x:Name ="txtModindicator" HorizontalAlignment="Left" Margin="433,181,0,0" TextWrapping="Wrap" Text="-" FontSize="20" FontWeight="Bold" VerticalAlignment="Top"/>
<TextBlock x:Name ="txtResindicator" HorizontalAlignment="Left" Margin="663,182,0,0" TextWrapping="Wrap" Text="=" FontSize="20" FontWeight="Bold" VerticalAlignment="Top"/>
</Grid>
</Grid>
now the shortened c# code:
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace SomeWPF
{
/// <summary>
/// Interaction logic for ModifyWindow.xaml
/// </summary>
public partial class MainWindow : INotifyPropertyChanged
{
public enum Mymode
{
add,
pull,
modify
}
public Mymode mode;
public MainWindow()
{
DataContext = this;
InitializeComponent();
CurrentStock = 5;
RbtPull.IsChecked = true;
ModEntry = 1;
}
private void ModeRadio_Checked(object sender, RoutedEventArgs e)
{
if (sender != null)
{
if (sender.Equals(RbtAdd))
{
mode = Mymode.add;
txtModindicator.Text = "+";
txtComment.Text = "Add";
lblOperation.Content = "Stock to Add:";
}
else if (sender.Equals(RbtPull))
{
mode = Mymode.pull;
txtModindicator.Text = "-";
txtComment.Text = "Pull";
lblOperation.Content = "Stock to Pull:";
}
else
{
mode = Mymode.modify;
txtModindicator.Text = "~";
lblOperation.Content = "Corrected Quantity:";
txtComment.Text = "Mod";
}
TxtEntry_TextChanged(sender, null);
}
}
private void TxtEntry_TextChanged(object sender, TextChangedEventArgs e)
{
if (mode == Mymode.add)
{
NewStock = CurrentStock + ModEntry;
}
else if (mode == Mymode.pull)
{
NewStock = CurrentStock - ModEntry;
}
else
{
NewStock = ModEntry;
}
}
#region Binding Stuff
private int _newstock;
public int NewStock
{
get
{
return _newstock;
}
set
{
if (_newstock != value)
{
_newstock = value;
OnPropertyChanged();
}
}
}
private int _modentry;
public int ModEntry
{
get
{
return _modentry;
}
set
{
if (_modentry != value)
{
_modentry = value;
OnPropertyChanged();
}
}
}
private int _currentstock;
public int CurrentStock
{
get
{
return _currentstock;
}
set
{
if (_currentstock != value)
{
_currentstock = value;
OnPropertyChanged();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}
So this window is a popup in a little program for an inventory storage for the users to enter movements of the inventory.
everything loads fine so far and I now wanted to do the quite simple calculation part. with "old" winforms c# you'd just take the values and update the text property of the result "manually" but of course we (I) want to learn new stuff and do stuff with data binding.
The code also does the calculation, but the trigger is somehow not what I want.
let's say current stock is 5
when window loads, the mode is set to Pull (RbtPull) and the user entry (Binding to ModEntry) is set to 1 via code. The NewStock therefore should be 4 which displays correctly. (yey)
Also the comment field (for debugging for now) displays the ModEntry value 1.
so far so good.
Now I enter 3 in the Stock to Pull field, but nothing happens. (I want it to react "realtime"). The new Stock is still displayed as 4, the comment is still displayed as 1.
When I leave the field (and click into the comment field) - the property change is detected and the Comment Field shows also 3 (=ModEntry) - so it's not "realtime" but only triggers when the field is losing focus, but that would be also acceptable.
The real problem is: The new Stock stays 4 and does not calculate.
Now when I enter the Stock to Pull field again and change the value to let's say 5, the New Stock field updates to 2 (so to the value I entered before 5-3=2)
Overwriting the field with again 5 will change the new Stock to 0.
So it's always "one step behind".
From what I have found i have an idea, that I need some kind of Binding Converter instead of my method of calculating things, but I can't really find anything suitable and am not familiar enough yet with data binding. Have tried out some things already directly in the binding variable code but none worked. If anyone could hint me in the right direction I'd be very thankful. (don't need a silver plate solution but just an idea what way to search (e.g. if the sort of binding I use makes sense at all or if there's something I have to add etc.).
Thanks a lot!
PS: of course if someone is motivated to give a silver plate solution I'd also be grateful. :) - and sorry for the bad english, no native speaker.
#nosale 's second link (see comments) provided the answer to the Problem.
Setting both XAML fields txtEntry and the Result field to UpdateSourceTrigger=PropertyChanged solved the issue.
so the correct block Looks like this now without changes to the c# code:
<Label Content="Current stock:" HorizontalAlignment="Left" Margin="289,156,0,0" VerticalAlignment="Top"/>
<Label x:Name ="lblOperation" Content="Stock to Pull:" HorizontalAlignment="Left" Margin="507,156,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtEntry" HorizontalAlignment="Left" Height="32" Margin="489,181,0,0" TextWrapping="Wrap" TextAlignment="Center" Text="{Binding Path=ModEntry, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120" FontSize="20" FontWeight="Bold" TextChanged="TxtEntry_TextChanged"/>
<Label Content="New Stock" HorizontalAlignment="Right" Margin="714,156,0,0" VerticalAlignment="Top" Width="68"/>
<TextBlock Text="{Binding Path=NewStock, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Right" Margin="0,186,10,0" TextAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="20" FontWeight="Bold" Width="68"/>
<TextBox x:Name="txtComment" HorizontalAlignment="Left" Height="86" Margin="289,233,0,0" TextWrapping="Wrap" Text="{Binding Path=ModEntry}" VerticalAlignment="Top" Width="493"/>
<Label Content="Comment:" HorizontalAlignment="Left" Margin="289,207,0,0" VerticalAlignment="Top"/>
Reason is, that textboxes have a Default UpdateSourceTrigger=LostFocus and not PropertyChanged to prevent updates with user having entered typos.
something new learned: WPF is cool and automatically handles non plausible values like null or strings and marks the field red! :)
thanks again for the links!

Datagrid not showing data in wpf with c#

I am trying to use a dataGrid in WPF with c#. But I cannot get my datagrid to show any of my data in the table when I run my program in debug mode. I have this code executing when the datagrid loads. But all I see is an empty square.
private void dataGrid1_Loaded(object sender, RoutedEventArgs e)
{
var items = new List<SaveTable>();
items.Add(new SaveTable("A" , 0));
items.Add(new SaveTable("B" , 0));
items.Add(new SaveTable("C" , 0));
items.Add(new SaveTable("D" , 0));
items.Add(new SaveTable("E" , 0));
var grid = sender as DataGrid;
grid.ItemsSource = items;
}
I save a class named SaveTable which looks like this:
class SaveTable
{
public string Name { get; set; }
public double Value { get; set; }
public SaveTable(string name, double value)
{
this.Name = name;
this.Value = value;
}
}
I got this code format online and it seems like everything is right? any suggestions?
here is the xaml code for that window
<Window x:Class="RobustCalculator.Storage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Storage" Height="499" Width="546" Activated="Window_Activated" Loaded="Window_Loaded">
<Grid>
<TextBlock Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBlock1" Text="A" VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Margin="24,10,0,306" Name="valueA" Width="120" TextChanged="valueA_TextChanged" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="24,39,0,0" Name="valueB" VerticalAlignment="Top" Width="120" TextChanged="valueB_TextChanged" />
<TextBlock Height="20" HorizontalAlignment="Left" Margin="10,42,0,0" Name="textBlock2" Text="B" VerticalAlignment="Top" Width="20" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="24,71,0,0" Name="valueC" VerticalAlignment="Top" Width="120" TextChanged="valueC_TextChanged" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="10,72,0,0" Name="textBlock3" Text="C" VerticalAlignment="Top" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="10,101,0,0" Name="textBlock4" Text="D" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="24,98,0,0" Name="valueD" VerticalAlignment="Top" Width="120" TextChanged="valueD_TextChanged" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="24,130,0,0" Name="valueE" VerticalAlignment="Top" Width="120" TextChanged="valueE_TextChanged" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="11,131,0,0" Name="textBlock5" Text="E" VerticalAlignment="Top" />
<DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Margin="235,130,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="200" SelectionChanged="dataGrid1_SelectionChanged" Loaded="dataGrid1_Loaded" />
</Grid>
I added a breakpoint and the code isn't being executed. I am using the Loaded event, should I be using a different event?
Your code works fine with Loaded event but at the moment you set AutoGenerateColumns="False" and don't define columns. You need to either define columns manually
<DataGrid AutoGenerateColumns="False" ... Loaded="dataGrid1_Loaded">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Header="Name"/>
<DataGridTextColumn Binding="{Binding Value}" Header="Value"/>
</DataGrid.Columns>
</DataGrid>
or let it auto generate columns
<DataGrid AutoGenerateColumns="True" ... Loaded="dataGrid1_Loaded"/>

How to bind or get properties from User Control

have a bit of a problem understanding some usercontrol interaction:
I've built this color picker. I have four sliders that change the color in Red, Green, & Blue (RGB and all that) as well as its opacity(alpha). When I slide these back and forth, I get a live response from the previewColor Rectangle(the fill color of the rectangle changes as I slide any of the sliders). All these elements are inside my user control.
In my main window, I have two signigicant elements, a blank canvas and a "change canvas" button. Ideally, when I play with the sliders in my user control and find a color i like, I would just click the ChangeCanvas button and the canvas background would change to match the current color of the previewRectangle.
The problem is that I cannot figure out how to bind the current color of the previewRectangle to a button action that my canvas will accept. Is there any way to bind the background of my canvas to the fill of my rectangle via button action? Or is it better to pass the color property from the usercontrol to the mainwindow?
Main Window XAML & Code
<Window x:Class="C_ShapeCanvasV2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:schafec="clr-namespace:C_ShapeCanvasV2.SControl"
Title="ColorCanvas" Height="393" Width="729">
<Grid Background="Gray">
<Canvas Name="MainCanvas" MouseLeftButtonDown="MainCanvas_MouseLeftButtonDown" ClipToBounds="True" MouseRightButtonDown="MainCanvas_MouseRightButtonDown" Background="White" HorizontalAlignment="Left" Height="363" VerticalAlignment="Top" Width="305"/>
<!-- user control -->
<schafec:ColorControls x:Name="colorControls" Margin="330,21,23,189"/>
<!-- user control -->
<Button Name="ChangeCanvas" Click="ChangeCanvas_Click" Content="Change Canvas" HorizontalAlignment="Left" Margin="456,209,0,0" VerticalAlignment="Top" Width="158" />
<Button Name="clearButton" Click="clearButton_Click" Content="Clear" HorizontalAlignment="Left" Margin="498,236,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
namespace C_ShapeCanvasV2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
//---------------------------------------------------------------------------------------------//
private void ChangeCanvas_Click(object sender, RoutedEventArgs e)
{
//attempted -- not working
//MainCanvas.Background = new SolidColorBrush(clr);
}
private void clearButton_Click(object sender, RoutedEventArgs e)
{
MainCanvas.Children.Clear();
MainCanvas.Background = new SolidColorBrush(Colors.White);
}
}
}
UserControl XAML & Code
<UserControl x:Class="C_ShapeCanvasV2.SControl.ColorControls"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="153" Width="368">
<Grid Name="mainGrid" Background ="Gray" Margin="0,6,0,-6">
<!-- just an image under the previewRectangle to provide contrast for opacity purposes -->
<Rectangle Name="underImage" HorizontalAlignment="Left" Height="58" Margin="24,52,0,0" Stroke="Black" VerticalAlignment="Top" Width="64">
<Rectangle.Fill>
<ImageBrush ImageSource ="/C_ShapeCanvasV2;component/Images/ops.png"/>
</Rectangle.Fill>
</Rectangle>
<!-- PreviewColor Rectangle -->
<Rectangle Name="previewColor" HorizontalAlignment="Left" Height="58" Margin="24,52,0,0" Stroke="Black" VerticalAlignment="Top" Width="64"/>
<Slider Name="redSlider" Minimum="0" ValueChanged="redSlider_ValueChanged" IsSnapToTickEnabled="True" Maximum="255" TickFrequency="1" HorizontalAlignment="Left" Margin="211,36,0,0" VerticalAlignment="Top" Width="104"/>
<Slider Name="greenSlider" Minimum="0" ValueChanged="greenSlider_ValueChanged" IsSnapToTickEnabled="True" Maximum="255" TickFrequency="1" HorizontalAlignment="Left" Margin="211,61,0,0" VerticalAlignment="Top" Width="104" />
<Slider Name="blueSlider" Minimum="0" ValueChanged="blueSlider_ValueChanged" IsSnapToTickEnabled="True" Maximum="255" TickFrequency="1" HorizontalAlignment="Left" Margin="211,86,0,0" VerticalAlignment="Top" Width="104"/>
<Slider Name="alphaSlider" Minimum="0" ValueChanged="alphaSlider_ValueChanged" IsSnapToTickEnabled="True" Maximum="255" TickFrequency="1" HorizontalAlignment="Left" Margin="211,111,0,0" VerticalAlignment="Top" Width="104"/>
<Label Content="Red" HorizontalAlignment="Left" Margin="112,34,0,0" VerticalAlignment="Top"/>
<Label Content="Green" HorizontalAlignment="Left" Margin="112,57,0,0" VerticalAlignment="Top"/>
<Label Content="Blue" HorizontalAlignment="Left" Margin="112,82,0,0" VerticalAlignment="Top"/>
<Label Content="Alpha" HorizontalAlignment="Left" Margin="112,107,0,0" VerticalAlignment="Top"/>
<Label Name="redLabel" Content="{Binding ElementName= redSlider, Path=Value}" HorizontalAlignment="Left" Margin="318,32,0,0" VerticalAlignment="Top"/>
<Label Name="greenLabel" Content="{Binding ElementName= greenSlider, Path=Value}" HorizontalAlignment="Left" Margin="318,59,0,0" VerticalAlignment="Top"/>
<Label Name="blueLabel" Content="{Binding ElementName= blueSlider, Path=Value}" HorizontalAlignment="Left" Margin="318,82,0,0" VerticalAlignment="Top"/>
<Label Name="alphaLabel" Content="{Binding ElementName= alphaSlider, Path=Value}" HorizontalAlignment="Left" Margin="318,107,0,0" VerticalAlignment="Top"/>
</Grid>
namespace C_ShapeCanvasV2.SControl
{
public partial class ColorControls : UserControl
{
public Color clr;
public ColorControls()
{
InitializeComponent();
}
private void changeColorSystem()
{
clr = Color.FromArgb(Convert.ToByte(alphaSlider.Value), Convert.ToByte(redSlider.Value), Convert.ToByte(greenSlider.Value), Convert.ToByte(blueSlider.Value));
previewColor.Fill = new SolidColorBrush(clr);
}
//public void setColor(Color g)
//{
// Color c = g;
//}
//public Color getColor()
//{
// return clr;
//}
private void alphaSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
changeColorSystem();
}
private void blueSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
changeColorSystem();
}
private void greenSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
changeColorSystem();
}
private void redSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
changeColorSystem();
}
}
}
That would not be clean in my opinion, but this piece of code should work:
private void ChangeCanvas_Click(object sender, RoutedEventArgs e)
{
MainCanvas.Background = colorControls.previewColor.Fill;
}
Still you should really inform yourself about MVVM architecture to do it in a more clean/testable way.
Hope it helps.

adding row dyanamicaly to gridview in WPF

Please help me with the following code,I want to add a row inputted by user to a gridview.
I am able to add a row but its empty!!Please help.it worked in windows forms but its not working with WPF.
private void button1_Click(object sender, RoutedEventArgs e)
{
GetGridView();
}
private void GetGridView()
{
string[] row0 = {textBox1.Text,"Beatles" };
dataGrid1.Items.Add(row0);
dataGrid1.Columns[0].DisplayIndex = 0;
dataGrid1.Columns[1].DisplayIndex = 1;
}
//////////////
sure,here it is
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="964">
<Grid>
<DataGrid AutoGenerateColumns="False" Height="274" HorizontalAlignment="Left" Margin="509,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="239" DataContext="{Binding}" ItemsSource="{Binding}" ItemStringFormat="{Binding}" SelectedIndex="-1" SelectionChanged="dataGrid1_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Header="Header1" />
<DataGridTextColumn Header="Header" />
</DataGrid.Columns>
</DataGrid>
<TextBox Height="23" HorizontalAlignment="Left" Margin="184,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" TextChanged="textBox1_TextChanged" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="184,187,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="184,125,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="184,66,0,0" Name="textBox4" VerticalAlignment="Top" Width="120" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="414,231,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
Edit: You bound the ItemsSource of the DataGrid, you cannot add items to the grid itself while that is the case, add the items to the bound collection (which is what i originally suggested)
I would not suggest you do anything like that. In WPF you should bind your controls to the data, that way you can change the source-collection and the grid will get updated automatically, which is less messy than using any method like DataGrid.Items.Add which accepts input of type object.
e.g.
Xaml:
<DataGrid ItemsSource="{Binding GridData}" Name="DGrid"/>
<TextBox Name="TB" Width="100"/>
<Button Content="Add" Click="Button_Click"/>
Code:
private ObservableCollection<Employee> gridData = new ObservableCollection<Employee>();
public ObservableCollection<Employee> GridData
{
get { return gridData; }
}
private void Button_Click(object sender, RoutedEventArgs e)
{
GridData.Add(new Employee(TB.Text, "Beatles?"));
}

Categories