This question already has answers here:
WPF Image Command Binding
(5 answers)
Closed 4 years ago.
I am new to C# and WPF, and I want to make something like Movie Library with MySQL as a database to show the image and the title using Stackpanel.
I don't know how to add click event on an image programmatically Because I'm not using the image in Xaml.
Also, can Stackpanel have 3x3 or 4x4 grid instead only have 1 column?
Screenshot of my program:
Here is My Code
public void FillData()
{
int id = 1;
for (int i = id; i < 100; i++)
{
MySqlCommand cmd;
cmd = koneksi.CreateCommand();
cmd.CommandText = "select * from movie_list where id_movie = '" + i + "'";
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
if (ds.Tables[0].Rows.Count == 1)
{
string cover = (String)(ds.Tables[0].Rows[0]["cover"]);
string titles = (String)(ds.Tables[0].Rows[0]["title"]);
StackPanel Sp = sp;
StackPanel Sp2 = sp2;
Sp.Orientation = Orientation.Horizontal;
Sp2.Orientation = Orientation.Horizontal;
var picture = new Image
{
Name = "pb" + i,
Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + cover, UriKind.Absolute)),
RenderSize = new Size(100,150),
Margin = new Thickness(20,0,20,0),
};
var title = new Label
{
Name = "sp" +i,
Content = titles,
Width = 120,
Margin = new Thickness(10, 0, 20, 0),
HorizontalContentAlignment = HorizontalAlignment.Center,
};
Sp.Children.Add(picture);
Sp2.Children.Add(title);
}
}
}
You really should separate the data access code from your UI code.
In the UI, use an ItemsControl with the ItemsSource set to your DataSet. Set the ItemsTemplate to a DateTemplate with the controls you require for each item.
Using a ListBox will handle the item selection rather than worrying about click events for the Image and other controls.
If you want a grid layout rather than a straight linear list, you can use a UniformGrid as the ItemsPanelTemplate.
You can add an event handler to the picture object you're creating:
picture.MouseUp += (s, e) => {
// Do something funny.
};
var picture = new Image();
picture.MouseLeftButtonUp += (s, e) =>
{
//[your code goes here]
};
Besides that you absolutely shouldn't mix your database code with your UI code, you can just subscribe to the MouseDown (or MouseUp, if it should be like a click) event on your Image and execute whatever you want:
picture.MouseDown += (sender, args) =>
{
Foo();
};
Related
I am working on a Xamarin.Android proyect, I am filling a list view with a Web Service query. Now I need to access the listview's items values which I will use for future queries.
This is how I fill my listView
WebReference.ToDoWS cliente = new ToDoWS();
DataTable tabla = new DataTable();
tabla = cliente.ObtenerTareas();
ListView listado = FindViewById<ListView>(Resource.Id.tareas);
if (tabla.Rows.Count > 0)
{
List<string> tareas = new List<string>();
for (int i = 0; i < tabla.Rows.Count; i++)
{
tareas.Add(tabla.Rows[i][1].ToString());
}
ArrayAdapter<string> adaptador = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, tareas);
listado.Adapter = adaptador;
}
Here's a pick of how it looks
Is there a way to access the text values inside each row? I tried with the SetOnClickListener but I don't know how to use it at all.
I was able to access the item index with the following:
listado.ItemClick += (sender, e) =>
{
string index = e.Position.ToString();
Toast.MakeText(this, "Click" + index, ToastLength.Long).Show();
};
Documentation Referece: https://learn.microsoft.com/en-us/xamarin/android/internals/api-design#Events_and_Listeners
I am trying to display my List in a ListView using a GridView. I am using dynamic binding as my list view changes. it's gridview for other purposes.
I have already used similar code twice and had no issues. I have checked every variable using a debugger and executed the code step by step and everything seemed normal.
// the code of the function
public void loadPairsIntoListView(object sender, RunWorkerCompletedEventArgs args)
{
XmlComparator xComp = XmlComparator.getInstance();
List<ComparePair> listOfPairs = xComp.getListOfPairs();
multiCompareListOfCompared.ItemsSource = listOfPairs;
multiCompareModelLabel.Content = listOfPairs[0].model.trueName + " " + listOfPairs[0].model.envName.ToUpper();
GridView myGridView = new GridView();
myGridView.ColumnHeaderToolTip = "Objet recap";
GridViewColumn gvc1 = new GridViewColumn();
gvc1.DisplayMemberBinding = new Binding("compared.trueName");
gvc1.Header = "nom";
gvc1.Width = 100;
myGridView.Columns.Add(gvc1);
GridViewColumn gvc2 = new GridViewColumn();
gvc2.DisplayMemberBinding = new Binding("compared.envName");
gvc2.Header = "environnement";
gvc2.Width = 100;
myGridView.Columns.Add(gvc2);
GridViewColumn gvc3 = new GridViewColumn();
gvc3.DisplayMemberBinding = new Binding("anomalies.Count");
gvc3.Header = "Ecarts";
gvc3.Width = 100;
myGridView.Columns.Add(gvc3);
multiCompareListOfCompared.View = myGridView;
Log.S();
}
// the class i'm trying to bind
public class ComparePair
{
public XmlFile model;
public XmlFile compared;
public List<int> anomalies;
private const int diffOnlyMarge = 10;
/// Methods ...
}
//
I am getting an output where the List is actually binded to the ListView (i can click on and use every row) but the rows are actually empty. I am getting all the three columns with their names but their rows has no "visual content" while still has the object bind to it.
What i am expecting is to see the actual values i have bind to each column.
I hope i have been clear enough. Tell me if you need more precision.
This question already has answers here:
Xamarin Forms - how to add background image to a content page
(2 answers)
Closed 5 years ago.
Guys Im having a little trouble trying to make an image the background for a certain page. Im trying to do it via the embedded method mentioned here:https://developer.xamarin.com/guides/xamarin-forms/user-interface/images/#Embedded_Images
.For some reason when I run the app, it crashes. I think something went wrong at 'BackgroundImage =' line cause I kinda guessed when I did that. When I comment out that line, the app loads fine, just without the background image. I used build action = content and copy to output directory as settings...
public class LoginPage : ContentPage
{
Entry emailBox = new Entry();
Entry passwordBox = new Entry();
Button createAccount = new Button();
Button forgotPassword = new Button();
StackLayout layout = new StackLayout();
Image embeddedImage = new Image { Source = ImageSource.FromResource("Charity.Properties.hands-426x640.jpg") };
public LoginPage()
{
Title = "Login";
BackgroundColor = Color.LightPink;
BackgroundImage = "Charity.Properties.hands-426x640.jpg";
emailBox.Placeholder = "email";
emailBox.BackgroundColor = Color.LightPink;
emailBox.PlaceholderColor = Color.HotPink;
passwordBox.Placeholder = "password";
passwordBox.BackgroundColor = Color.LightPink;
passwordBox.PlaceholderColor = Color.HotPink;
passwordBox.IsPassword = true;
createAccount.Text = "create an account";
createAccount.TextColor = Color.HotPink;
createAccount.Font = Font.SystemFontOfSize(NamedSize.Medium);
createAccount.BorderWidth = 0;
createAccount.HorizontalOptions = LayoutOptions.Center;
createAccount.VerticalOptions = LayoutOptions.CenterAndExpand;
forgotPassword.Text = "recover password";
forgotPassword.TextColor = Color.HotPink;
forgotPassword.Font = Font.SystemFontOfSize(NamedSize.Medium);
forgotPassword.BorderWidth = 0;
forgotPassword.HorizontalOptions = LayoutOptions.Center;
forgotPassword.VerticalOptions = LayoutOptions.CenterAndExpand;
layout.VerticalOptions = LayoutOptions.End;
layout.BackgroundColor = Color.LightPink;
layout.Children.Add(emailBox);
layout.Children.Add(passwordBox);
layout.Children.Add(createAccount);
layout.Children.Add(forgotPassword);
Content = layout;
}
}
This works, after much trial and error I only needed to add the image to both .Droid(drawable) and .iOS(resources) folders. I had not added it to the iOS folder previously, hence it kept crashing when I ran on an iOS simulator.
Image image = new Image();
public LoginPage()
{
Title = "Login";
BackgroundImage = "hands426x640.jpg";}
According to codes below, I can gather my stored images from my SQL server table and put a CheckBox under each of them.My aim is to use these CheckBoxes to select images above them and take an action with another user control (i.e. button for erasing them from DB or copying them to another directory).I tried to create an event handler but cannot achieve with my level of c# knowledge. I kindly ask for a solution and guidance for better understanding.
private void button4_Click(object sender, EventArgs e)
{
PictureBox[] pba = new PictureBox[100];
SqlConnection con4 = new SqlConnection(DBHandler.GetConnectionString());
SqlCommand cmd4 = new SqlCommand("ReadAllImage", con4);
cmd4.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da2 = new SqlDataAdapter(cmd4);
DataSet dt2 = new DataSet("ImageData");
da2.SelectCommand = cmd4;
da2.Fill(dt2);
int a = comboBox1.Items.Count;
byte[] xdata = new byte[0];
CheckBox[] chckbx = new CheckBox[400];
for (var i = 0; i < a; i++)
{
DataRow myRow2 = null;
if (myRow2 == null)
{
myRow2 = dt2.Tables[0].Rows[i];
xdata = (byte[])myRow2["ImageData"];
MemoryStream stream2 = new MemoryStream(xdata);
PictureBox npb = new PictureBox();
npb.Size = new Size(60, 60);
int b = i / 5;
npb.Location = new Point(420 + (i % 5) * 70, 20 + (90 * b));
npb.Image = Image.FromStream(stream2);
npb.SizeMode = PictureBoxSizeMode.StretchImage;
pba[i] = npb;
this.Controls.Add(pba[i]);
CheckBox chckbxx = new CheckBox();
chckbxx.Location = new Point(420 + (i % 5) * 70, 80 + (90 * b));
chckbxx.AutoSize = true;
chckbx[i] = chckbxx;
this.Controls.Add(chckbx[i]);
}
my example output after running the codes.I wanna use the chekckboxes like in this pic.I actually managed to create them like this but couldn't make them related with images above them.
make a custom control holding the image AND the checkbox.
then both are always connected:
logically and programmatically.
samples for custom controls :
https://msdn.microsoft.com/en-us/library/ff723977%28v=expression.40%29.aspx
http://www.codeproject.com/Articles/2016/Writing-your-Custom-Control-step-by-step
I'm new to Visual Studio and fixing a small bug in an application.
The combo box which exist is editable with DropDown eventhough it is has databinding. Because If I deleted a value, it would not save the change, I made it into a DropDownList; however, now I do not have the option of using null.
I have searched around and have been reading that I can insert an item before databinding.
I have the bits and peices of the codes from different functions for this combo box. Not sure where to exactly make the change.
if someone could point me in the right direction, that'd be great.
private System.Windows.Forms.ComboBox cmbSecCSR;
//----------------
this.cmbSecCSR = new System.Windows.Forms.ComboBox();
//---------------------
// cmbSecCSR
//
this.cmbSecCSR.AccessibleRole = System.Windows.Forms.AccessibleRole.TitleBar;
this.cmbSecCSR.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
this.cmbSecCSR.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
this.cmbSecCSR.DataSource = this.csrBindingSource2;
this.cmbSecCSR.DisplayMember = "Name";
this.cmbSecCSR.FormattingEnabled = true;
this.cmbSecCSR.Location = new System.Drawing.Point(112, 26);
this.cmbSecCSR.Margin = new System.Windows.Forms.Padding(0);
this.cmbSecCSR.MaxDropDownItems = 10;
this.cmbSecCSR.Name = "cmbSecCSR";
this.cmbSecCSR.Size = new System.Drawing.Size(184, 21);
this.cmbSecCSR.TabIndex = 2;
this.cmbSecCSR.ValueMember = "Username";
this.cmbSecCSR.TextChanged += new System.EventHandler(this.comboBox_TextChanged);
this.cmbSecCSR.Enter += new System.EventHandler(this.cmbBox_Entered);
//
// csrBindingSource2
//
this.csrBindingSource2.DataMember = "CSR";
this.csrBindingSource2.DataSource = this.productionDS;
//-------------------------
//loadUnboundData();
cmbSecCSR.DataBindings.Add("SelectedValue", productionMasterBindingSource, "CSR2", true, DataSourceUpdateMode.OnPropertyChanged);
My Problem is similar to this: http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/fa277629-a65b-4799-9400-364f6f771739/
which is why I decided to change it to DropDownList;however, I don't know how to add the NULL Value in the DropDownList as it is bounded.