I need to add selected items to this collection..
I select my item on a page and then appbar appears and i want on the appbar button tapped event to add item to collection that will be displayed on the other page..
private void Button_Tapped_1(object sender, TappedRoutedEventArgs e)
{
AllActors m = new AllActors();
ActorsObject objkt = itemGridView.SelectedItem;
m.allActors.Add(objkt);
}
this doesnt work... here are my classes:
public class AllActors : LivingDataCommon
{
public AllActors()
: base(String.Empty, String.Empty)
{
}
public AllActors(String ID, String title)
: base(ID, title)
{ }
private ObservableCollection<ActorsObject> _AllActors = new ObservableCollection<ActorsObject>();
public ObservableCollection<ActorsObject> allActors
{
get { return this._AllActors; }
}
}
Problem one: compiler error "Cannot implicitly convert type"
ActorsObject objkt = itemGridView.SelectedItem;
The GridView's SelectedItem property returns an object. You're trying to assign that to a variable of type ActorsObject, and the compiler can't assume that this is okay. You have to tell it...
ActorsObject objkt = (ActorsObject)itemGridView.SelectedItem;
The compiler's error message specifically asked "are you missing a cast?" and pointed to this line. That's useful information - it's just told you what was wrong and suggested how to fix it. Always read compiler errors and think about what they're telling you, don't just reduce it to "it doesn't work".
Problem two: "it doesn't do what I want"
private void Button_Tapped_1(object sender, TappedRoutedEventArgs e)
{
AllActors m = new AllActors();
ActorsObject objkt = itemGridView.SelectedItem;
m.allActors.Add(objkt);
}
Read this carefully and think about what it's doing. On the first line of the function, you're creating a new AllActors object and assign it to 'm'. On the third line, you make a change to that object. But then your function ends, and you've thrown that object away!
It's more likely that to achieve what you want you need to create the AllActors object as a field on your window class. Then you can keep the object around, bind to it, and any changes to it will be kept. Something like this, although you will also need some UI to display this data.
private AllActors _m = new AllActors();
private void Button_Tapped_1(object sender, TappedRoutedEventArgs e)
{
ActorsObject objkt = (ActorsObject)itemGridView.SelectedItem;
_m.allActors.Add(objkt);
}
You need to change
ActorsObject objkt = itemGridView.SelectedItem;
to
ActorsObject objkt = (ActorsObject)itemGridView.SelectedItem;
you are missing a cast there.
Try this:
ActorsObject objkt = (ActorsObject ) itemGridView.SelectedItem;
m.allActors.Add(objkt);
or
ActorsObject objkt = itemGridView.SelectedItem as ActorsObject;
m.allActors.Add(objkt);
You have to cast SelectedItem, since it is object not ActorsObject
Related
I created a class and I want to use the constructor of the class Rtb(),
public class Rtb
{
public RichTextBox newRTB;
public Rtb()
{
newRTB = new RichTextBox();
newRTB.IsReadOnly = true;
newRTB.MouseDoubleClick += new MouseButtonEventHandler(newRTB_MouseDoubleClick);
}
private void newRTB_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
.....
}
}
In below code, i created an object of Rtb(),but this object can not assigned to newBUC.Child, an error after run: Cannot implicitly convert type 'WpfApplication1.Rtb' to 'System.Windows.UIElement'
private void menu_Click(object sender, RoutedEventArgs e)
{
BlockUIContainer newBUC = new BlockUIContainer();
newBUC.Margin = new Thickness(50, 10, 50, 10);
mainMenu.Document.Blocks.Add(newBUC);
Rtb newnew = new Rtb();
newBUC.Child = newnew;
}
I tried to use to cast it, and use "as", like below, but it did not work. I think probably i need the right type to perform the assignment, how should i do?
newBUC.Child = newnew as BlockUIContainer;
newBUC.Child = (BlockUIContainer) newnew;
You can't add newnew as a Child, because you class does not inherit from UIElement. But what can you do is set Child to underlying RichTextBox called newRTB which inherits from UIElement
newBUC.Child = newnew.newRTB;
In my case I was missing a reference to PresentationFramework.dll
located on Program files* x86( reference assembles„Microsoft\FRamewrk\3.0\
or your FRamework version
This got me access to all dependencies on my XAML Partial class and therefore able to resolve
I hope this also can help on your coding, It worked for me.
I'm using the IntegerUpDown control from WPFExtendedToolkit
However, I'm unable to assign the event my function so that when the value is changed it will call my function. I'm pretty new to both c# and wpf so help is greatly appreciated. I've been trying to get it to work as show in a similar example here.
private IntegerUpDown m_argumentUpDown;
public IntArgumentOption(ArgumentOptionDescription argumentDesc) : base(argumentDesc)
{
m_argumentUpDown = new IntegerUpDown
{
Watermark = argumentDesc.m_watermark,
Increment = (int)argumentDesc.m_interval,
Minimum = (int)argumentDesc.m_minimum,
Maximum = (int)argumentDesc.m_maximum,
FormatString = "G",
SelectAllOnGotFocus = true,
MinWidth = 50,
FontSize = 10,
Margin = new System.Windows.Thickness(5, 0, 0, 0),
};
// COMPILER ERROR:
m_argumentUpDown.ValueChanged += new RoutedPropertyChangedEventHandler<int>(ArgumentChanged);
}
void ArgumentChanged(object sender, RoutedPropertyChangedEventArgs<int> e)
{
}
Doing this results in the compiler error:
error CS0029: Cannot implicitly convert type 'System.Windows.RoutedPropertyChangedEventHandler< int >' to 'System.Windows.RoutedPropertyChangedEventHandler< object >'
Here in the UpDownBase class (Xceed.wpfToolkit.dll) the method signature for ValueChanged is:
public event RoutedPropertyChangedEventHandler<object> ValueChanged;
hence in your code you have to declare a event handler where the generic is of type "Object" instead of int. Because of the mismatch in type the compiler is unable to implicitly convert to Int from object.
So change code as below
m_argumentUpDown.ValueChanged += new RoutedPropertyChangedEventHandler<object>(ArgumentChanged);
}
void ArgumentChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
//type caste e.newValue and e.OldValue
}
The following will work, I tested that. But I dont know if this is considered work around or creator of IntegerUpDown control meant it to be used this way.
m_argumentUpDown.ValueChanged += new RoutedPropertyChangedEventHandler<object>(ArgumentChanged);
//or you can change above line to following for brevity. ReSharper always suggesting me to do this
//m_argumentUpDown.ValueChanged += ArgumentChanged;
void ArgumentChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
//you need to cast New and Old value to int since both are of type object now
int newVal = (int)e.NewValue;
int oldVal = (int)e.OldValue;
}
I am talking in the context of event handler in a C# windows forms, but I'm assuming the answer could be used anywhere in C#.
To give an example, I have a form that has many check boxes that each activate a button. The CheckedChanged event is handled by a function that is very similar for each CheckBox and it looks something like this right now:
private void acheckbox_CheckedChanged(object sender, EventArgs e)
{
int uniquetocheckbox = 12345;
if(acheckbox.CheckedChanged)
{
ThisFunction(uniquetocheckbox, true);
AssociatedButton.Enabled = true;
}
else
{
ThisFunction(uniquetocheckbox, false);
AssociatedButton.Enabled = false;
}
}
There are a lot of these check boxes and I'm trying to cut and past the code for each and make as few changes as possible so I want to do something like this :
private void acheckbox_CheckedChanged(object sender, EventArgs e)
{
int uniquetocheckbox = 12345;
if((CheckBox)sender.Checked) //CHANGE HERE
{
ThisFunction(uniquetocheckbox, true);
AssociatedButton.Enabled = true;
}
else
{
ThisFunction(uniquetocheckbox, false);
AssociatedButton.Enabled = false;
}
}
This does not work. The easy work around is this :
private void acheckbox_CheckedChanged(object sender, EventArgs e)
{
int uniquetocheckbox = 12345;
CheckBox cb = (CheckBox)sender;
if(cb.Checked) //CHANGE HERE
{
ThisFunction(uniquetocheckbox, true);
AssociatedButton.Enabled = true;
}
else
{
ThisFunction(uniquetocheckbox, false);
AssociatedButton.Enabled = false;
}
}
But out of pure curiosity I am wondering if there is a way to do it in one line like the second example I gave. I would like to know because I think it looks better and is obviously 1 line shorter.
I think you're just missing a set of parenthesis. You want to cast to Checkbox, then get the properties of that:
if (((CheckBox)sender).Checked)
This will force the order of operations to cast first, then get the property from the cast result.
Sure, it's possible. You just missed another set of brackets:
if(((CheckBox)sender).Checked)
However, I wouldn't do this. Why? You don't want to cast again if you want to access the sender as a textbox again if you did it your way.
You can. For example:
object o;
o = new SomeType();
var prop = ((SomeType)o).SomeProperty;
It needs to be this:
if(((CheckBox)sender).Checked) //CHANGE HERE
But personally I like the way you've shown better. That way if it needs to be casted again, it's already been done.
Don't know any C# but ((CheckBox)sender).Checked) should work. In java the "." (member access) has higher priority than casting so putting the parenthesis like this should force the casting to happen first.
You just need a couple of more parenthesis in your if statement:
if (((CheckBox)sender).Checked)
{
...
}
It might be easy but im stuck!
i do have a class that Create one Command for me like:
As you can see this Encode will Return me bCommand!
Now im having a Button Function that i should call this bCommand and Assign it as a Value like:
So i got the Error that Bcommand Does not Exist in Current Context
I'm looking for any advice how i can Solve the Problem.
You need to assign the results of the Encode method to a variable.
private void btnModel_Click(object sender, EventArgs e)
{
byte[] bCommand = Encode("C11", "");
WriteData(bCommand); // bCommand will now exist in this context
}
Wierd behaviour when passing values to and from second form.
ParameterForm pf = new ParameterForm(testString);
works
ParameterForm pf = new ParameterForm();
pf.testString="test";
doesn't (testString defined as public string)
maybe i'm missing something? Anyway I'd like to make 2nd variant work properly, as for now - it returns null object reference error.
Thanks for help.
Posting more code here:
calling
Button ParametersButton = new Button();
ParametersButton.Click += delegate
{
ParameterForm pf = new ParameterForm(doc.GetElementById(ParametersButton.Tag.ToString()));
pf.ShowDialog(this);
pf.test = "test";
pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit);
};
definition and use
public partial class ParameterForm : Form
{
public string test;
public XmlElement node;
public delegate void ParameterSubmitResult(object sender, XmlElement e);
public event ParameterSubmitResult Submit;
public void SubmitButton_Click(object sender, EventArgs e)
{
Submit(this,this.node);
Debug.WriteLine(test);
}
}
result:
Submit - null object reference
test - null object reference
pf.ShowDialog(this); is a blocking call, so pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit); is never reached: switch the order.
Submit(this,this.node); throws a null object reference because no event is assigned to it (see above). Generally, you should always check first: if (Submit != null) Submit(this,this.node);
You should change ``pf.ShowDialog(this);topf.Show(this);` so that your main form isn't disabled while your dialog box is open, if that's what you want, or use the model below (typical for dialog boxes.)
I'm not sure what pf_Submit is supposed to do, so this might not be the best way to go about it in your application, but it's how general "Proceed? Yes/No" questions work.
Button ParametersButton = new Button();
ParametersButton.Click += delegate
{
ParameterForm pf = new ParameterForm(testString);
pf.ShowDialog(this); // Blocks until user submits
// Do whatever pf_Submit did here.
};
public partial class ParameterForm : Form
{
public string test; // Generally, encapsulate these
public XmlElement node; // in properties
public void SubmitButton_Click(object sender, EventArgs e)
{
Debug.WriteLine(test);
this.Close(); // Returns from ShowDialog()
}
}
When you want to use your second variant, you have to use a getString()-Method, where you can put the e.g. "testString". The way you wrote it, "testString" should be a method (and got brackets).
EDIT (a bit more precise):
You could write:
pf.getString(testString);
, if "pf" is an instance of your own class, otherwise you had to look up, whether you can retrieve a String in this class.
the thing was in line order :)
pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit);
and
pf.Test = "test";
should have been set before
pf.ShowDialog(this);
my mistake thingking that parameter can be passed after 2nd form was displayed
thnx for answers