I am currently creating a C# XAML Windows Store App I have successfully gotten the app to do what i want it to do which is when I click on a listviewitem the image to the right changes, however to do this for each listview item i am having to create a bitmap image for every listviewitem.
I find it very time consuming would anybody be able to recommend a type of method i could create to save me from writing this in every listviewitem Selection_changed.
BitmapImage PlatoImage = new BitmapImage(new Uri(this.BaseUri,"example.jpg"));
PhilosopherImage.Source = PlatoImage;
Below is the original method
private void PhilosopherList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (PhilosopherList.SelectedIndex ==1)
{
BitmapImage Aristotle = new BitmapImage(new Uri(this.BaseUri, "aristotle.png"));
PhilosopherImage.Source = Aristotle;
All i have done for the rest is insert 'else if' statements to compensate for the other 'selectedindex' I haven't created a method yet to save me time to create a new bitmap image for each listviewitem yet
It sounds like your listview items are strings.
Without using data binding, you can change the ListView items to objects that contain the string you want to display and the image url, then add a tostring() method that returns the string you want to display. Then your SelectionChanged handler can simply grab the image url from the selected item.
The listview item type would be something like this:
public class ItemType
{
public string DisplayString{get;set;}
public string ImageUrl{get;set;}
public override string ToString()
{
return DisplayString;
}
}
Related
I am trying to make a simple image viewer that loads images at start, creates Items with pulled SQL data and then creates Dictionary with all the Item parameters.
Then another method builds what's needed and fills StackPanel with UserControls (ITEM) that act as "links" to images.
Trying to add another image, from UserControl (NEW_ITEM) nested in View that is loaded to ContentControl in MainWindow.
public partial class fileUpload : UserControl
{
public fileUpload()
{
InitializeComponent();
}
string PathSource = #"SOURCE";
string PathToCopy = #"DESTINATION";
private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
PathSource = FU_source.Text;
if (!FU_fileName.Text.Contains("."))
{
MainWindow V = new MainWindow();
V.LoadCollection(); //This seems to work, but the content on MainWindow stays visible.
////Copy Inserted Item
File.Copy(PathSource,PathToCopy + FU_fileName.Text + ".png", true);
////Remove Item Preview From List
((ItemsControl)this.Parent).Items.Remove(this);
}
else
{
MessageBox.Show("ERR TEST", "ERR TEST HEADER");
}
}
}
Running LoadCollection() method does work OK if it's called from the MainWindow but does not work if called from fileUpload Class. (Program seems to do what's needed, but the user control won't disappear.)
Basically, it should refresh the whole process and refresh/refill StackPanel with new items.
I have encountered this problem many times and I think that I'm missing a key part in this.
TL;DR: I am trying to refresh MainWindows StackPanel with new Items, but the controls won't disappear if method taking care of this is called from UC Class.
I'm currently developing a WPF C# Application that contains some textbox validations. If field is valid it must show a ok validation image if not valid it must show a wrong validation image, like an image below.
My Problem is how to set visibility = visibility.Hidden for all images if I click on cancelar button or another button. I know set img1.visibility = visibility.Hidden;, img2.visibility = visibility.Hidden;, img3.visibility = visibility.Hidden;... Works but i need to create a function to do it. I believe that I create a List of Images and pass this List of parameter to a function works fine and I can use this function for other validations. So how can I do it?
Please check this article : Data Binding
If you implement data binding then you have just to bind properties:
<Image Source="..." Visibility="{Binding Img1Visibility}"/>
Implement ViewModel class via INotifyPropertyChanged
And then simply work with your Properties in code.
UPD
If you want to simply create function to work with your images then move your img1.visibility = visibility.Hidden;, img2.visibility = visibility.Hidden;, img3.visibility = visibility.Hidden; in separate function inside your MainWindow.xaml.cs file, you don't have to pass it as arguments as you work in one MainWindow class.
So simply:
private void Fun()
{
img1.visibility = visibility.Hidden;
img2.visibility = visibility.Hidden;
img3.visibility = visibility.Hidden;
}
And request your Fun() method from ClickButton handler.
Create an array of the image controls and iterate over it.
List<Image> _images = new List<Image>
{
img1,
img2,
...
};
void Cancelar()
{
foreach (var image in _images)
{
image.Visibility = Visibility.Hidden;
}
}
But still, the code is awful. Witness me SO.
I have an image name as a string. The real imagename on the form is called "image". So i get something like this:
image.Visibility = Visibility.Hidden;
string imageName = "image";
// need something here to make it usable...
changedImageName.Visibility = Visibility.Visible;
Now, a string can not be used in combination with the Visibility property.
I cant really find what i must make the string to, to make it usable for the visibility property.
If i see this page: http://msdn.microsoft.com/en-us/library/system.windows.visibility.aspx
Do I understand correct that I make it a "enum" ? And if yes, how do I get a string to that property?
EDIT 1
I see I have not been explaining it proper enough.
I forgot to mention I am using a WPF form.
on this form, I have put an image.
In the initialize part, the image get set to hidden.
so for example the imagename I named "Image"
so I use image.Visibility = Visibility.Hidden;
later on in my code, I want to make the image visible again, depending on what the user does.
so, instead if just using the name to get the image visible again, I want to use a string.
this string is looking exactly as the name of the image.
but i cant use the string in combination with the Visibility function.
but i cant find anywhere what i must make this string to, to be able to use that visibility option on it.
hope i explained a bit better now :).
Later on, i will have multiple images on the WPF window.
So the key is that i will use the string, that is corresponding with the name of the image.
Depending on what the user has input into the string, some image will or will not show.
EDIT 2
If you have:
String theName = ImageName.name
you can get the name of the image into a string, so you can do stuff with it.
i am looking for a way to do the exact opposite of this. So i want to go from a string, to that name, so after that i can use this to control the image again.
Edit 3
some example:
private void theButton_Click(object sender, RoutedEventArgs e)
{
//get the Name property of the button
Button b = sender as Button;
string s = b.Name;
MessageBox.Show("this is the name of the clicked button: " + s);
//the name of the image to unhide, is the exact same as the button, only with IM in front so:
string IM = "IM";
IM += s;
MessageBox.Show("this string, is now indentical to the name of the image i want to unhide, so this string now looks like: " + IM );
// now, this wont work, because i cant use a string for this, although the string value looks exactly like the image .name property
// so string IM = IMtheButton
// the image to unhide is named: IMtheButton.name
IM.Visibility = Visibility.Visible;
}
looks like you're using WPF, so you can create a boolean to visibility converter and use it with a boolean (and create a method that receives string if necessary) and just use:
<ContentControl Visibility="{Binding Path=IsControlVisible, Converter={StaticResource BooleanToVisibilityConverter}}"></ContentControl>
or any other converter...
check this links:
http://bembengarifin.wordpress.com/2009/08/12/setting-visibility-of-wpf-control-through-binding/
http://andu-goes-west.blogspot.com/2009/05/wpf-boolean-to-visibility-converter.html
http://msdn.microsoft.com/en-us/library/system.windows.controls.booleantovisibilityconverter.aspx
EDIT 1:
so then you will have to iterate over the images and check if your string is equals to name of the Image class.
something like this (not tested):
foreach (Image img in container.Items)
{
if img.Name == yourMagicallyString;
{
img.Visibility = Visibility.Visible;
}
else
{
img.Visibility = Visibility.Hidden;
}
}
If I understand correctly, you are trying to find a control based on the name or ID of the control. If so, try this:
Control changedImage = this.Controls.Find("image", false)[0];
Depending on what you are targeting and what version you might need to tweak a little
EDIT Updated per #Alexander Galkin comments about Find returning an array. There should definitely be some checking and whatnot but I'm leaving that up to the OP.
EDIT 2 For finding a control by name in WPF see this post.
The code I was looking for:
object item = FindName(IM);
Image test1 = (Image)item;
test1.Visibility = Visibility.Visible;
I want to make a ListView that has small images, that was taken from a scanner, inside of the ListView. (I have the scanning script done, and it saves the scanned image under C:/Temp/*.jpg. )
What I'm having trouble with is, I want the scanned images to be displayed inside of the ListView and when you click an image in the ListView it displayed the full Image in the PictureBox.
An Image of what i'm talking about.(tried to post the image inside of this post but rep isn't high enough)
I was thinking about having the images' location stored inside of an List array like
List<string> fileLocationArray = new List<string>();
foreach () {
...
string fileLoc = (#"C:\temp\" + DateTime.Now.ToString("yyyy-MM-dd HHmmss") + ".jpeg");
fileLocationArray.Add(fileLoc);
...
}
Then displaying the images inside the ListView using the List array.
Keep in mind that I plan on uploading these images to an FTP server. That's why I wanted to use a List array.
one more thing, they will be picture of document, not photos, if that means anything to you.
**Fill ListView :**
For(int i=0; i<fileLocationArray.Count();i++)
{
System.Windows.Controls.Image imgControl=new System.Windows.Controls.Image();
BitmapImage imgsrc = new BitmapImage();
imgsrc.BeginInit();
imgsrc.UriSource=fileLocationArray[i];
imgsrc.EndInit();
imgControl.source=imgsrc;
listView.Items.Add(imgControl);
}
**After filling ListView control create event listView SelectionChanged**
**imgContolShow // this control show selected image**
void listw_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
imgContolShow.Source = ((System.Windows.Controls.Image)listwiev.SelectedItem).Source;
}
Hi all i have a listbox MainListBox where i add items to dynamically.
Now i want to navigate to DetialsPage.xaml.cs when i choose an item in the listbox.
where i can then display my info about the selected item.
private void SetListBox()
{
foreach (ToDoItem todo in itemList)
{
MainListBox.Items.Add(todo.ToDoName);
}
}
MainListBox_SelectionChanged ("Generated by visual studio 2010 silverlight for windows 7 phone)
// Handle selection changed on ListBox
private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// If selected index is -1 (no selection) do nothing
if (MainListBox.SelectedIndex == -1)
return;
// Navigate to the new page
NavigationService.Navigate(new Uri("/DetailsPage.xaml?selectedItem=" + MainListBox.SelectedIndex, UriKind.Relative));
// Reset selected index to -1 (no selection)
MainListBox.SelectedIndex = -1;
}
in DetailsPage.xaml.cs is the next method. ("Generated by visual studio 2010 silverlight for windows 7 phone)
I'm aware that the below method does not do what i try.
// When page is navigated to set data context to selected item in list
protected override void OnNavigatedTo(NavigationEventArgs e)
{
string selectedIndex = "";
if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
{
int index = int.Parse(selectedIndex);
DataContext = App.ViewModel.Items[index];
}
}
I would like to access the selectedIndex and call my methods of my object that is in the MainListbox
so Basicly:
Mainlistbox => select item => send that item to details page => details page access the item and call methods on the item (object)
I'm sure this is a basic question tough it seems hard to find any specifics on it. i would like to add that this is my first windows phone 7 app.
There are many ways you can pass an object from page to page:
serialize and deserialize like Dennis said, but this, although feasable, is not practical, unless you want to save the object in isolated storage and retrieve it later.
Place an object in the App.cs class, which is accessible to all pages. Set your object in the master page, retrieve it from the Details page.
Code to put in App.cs: MyObject selectedObject;
Code to put in MasterPage.cs: application.selectedObject = MainListBox.selectedItem;
Code to put in DetailsPage.cs: MyObject selectedObject = application.seletedObject;
You can set the Object in the DataContext of your LayoutRoot, but i don't have the code for that on top of my head.
The answer here is simple - you cannot directly pass an object to another page. You can serialize it to JSON or XML and then deserialize it on the target page, but the serialized item will still have to be passed as a parameter.
Instead of sending the selectedindex as a query string parameter you could send the ID for the object or similar, something that uniquely can identify the object.
Then in the details page you could fetch the correct object from the same datasource that the main list box get its data from (in your case "itemList" which could come from e.g. IsolatedStorage).
If itemList is instantiated and kept only within the main page then you won't be able to fetch the item by ID from the details page. So in that case you'd need to move the itemList to some static or app level storage.
HTH