Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I need help for item add in the stackpanel.
My item name is "kanal" its wp8 imagebutton item.
Its my codes
public mainpage()
{
InitializeComponent();
foreach (var kanal in MainPage.kanalllarstatik)
{
mystackpanel.Children.Add(kanal);
}
}
I need add 130x130 pixel 3 button items per line like this:
Stackpanel only put one element per line so you need to put a horizontal stackpanel (in each line) and then add to it the three elements.
If you want a 130x130 control, you should use:
kanel.Height=130;
kanal.Width =130;
Code example
Test Data
List<Button> buttons = new List<Button>();
for (int i = 0; i < 16; i++)
{
buttons.Add(new Button
{
Height = 130,
Width = 130,
Content = new TextBlock
{
Text = i.ToString()
}
});
}
Algorithm
StackPanel horizontalStackPanel = new StackPanel
{
Orientation = Orientation.Horizontal
};
foreach (Button button in buttons)
{
horizontalStackPanel.Children.Add(button);
if (horizontalStackPanel.Children.Count == 3) //new line
{
myStackPanel.Children.Add(horizontalStackPanel);
horizontalStackPanel = new StackPanel
{
Orientation = Orientation.Horizontal
};
}
}
myStackPanel.Children.Add(horizontalStackPanel);
XAML
<StackPanel x:Name="myStackPanel"></StackPanel>
Result
I hope that this can help you.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a c# FlowLayoutPanel container to which I am adding a number of labels with Label.Text set to different values ie Label.Text = "ABCDEF".
What is the best way to search all the labels in the container to find the
a particular label with the Text = "ABCDEF"?
Thank You
You can find the label with text as follow:
foreach (var item in flowLayoutPanel1.Controls)
{
if (item is Label)
{
if ("ASDF" == ((Label)item).Text)
{
MessageBox.Show("found it");
}
}
}
Also if you know your component's name, you can search it as below:
foreach (var item in flowLayoutPanel1.Controls.Find("label1", true))
{
if ("ASDF" == ((Label) item).Text)
{
MessageBox.Show("found it");
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Display data from API, into WPF application.
Hello, Im creating an application for a questionnaire. It calls the data from the API and i would like to know what is the best way to display the information.
The information displayed will have to be "questionnaire type" and will be attempted by users and saved into database with the selected values.
Thanks
I already tried through textboxes that are dynamically created from a List, but setting the location of the textboxes tend to be full of errors
Please have a look at StackPanel
It stacks its child elements below or beside each other, dependening on its orientation.
With a stack panel you can place multiple elements atop of each other, see the sample from the link provided
<StackPanel>
<TextBlock Margin="10" FontSize="20">How do you like your coffee?</TextBlock>
<Button Margin="10">Black</Button>
<Button Margin="10">With milk</Button>
<Button Margin="10">Latte machiato</Button>
<Button Margin="10">Chappuchino</Button>
</StackPanel>
this will result in the following layout
You can create these dynamically. Take the following template
<StackLayout x:Name="QuestionStack">
<TextBlock Margin="10" FontSize="20" x:Name="QuestionTextBlock" />
</StackLayout>
and in your code behind
void DisplayQuestion(Question question)
{
QuestionTextBlock.Text = question.QuestionText;
foreach(var answer in question.Answers)
{
AddAnswerButton(answer);
}
}
private void AddAnswer(Answer answer)
{
QuestionStack.Children.Add(CreateButtonForAnswer(answer));
}
private Button CreateButtonForAnswer(Answer answer)
{
var button new Button()
{
Content = answer.Text,
Margin = 10
}
button.Click += (sender, eventArgs) =>
{
// handle button click
};
return button;
}
Please note: This is only one possibility. Without knowing more about your requirements it's hard to tell what you need exactly.
Edit:
Since you asked: You could for example create a custom control to display one question (I've replaced the Button with CheckBox and for sake of simplicity I've omitted the XAML, but it is not too hard to achieve the same results with XAML)
class QuestionControl : ContentControl
{
private Question question;
private StackLayout QuestionStackLayout { get; }
public QuestionControl()
{
QuestionStackLayout = new StackLayout();
QuestionTextBlock = new TextBlock()
{
Margin = 10,
FontSize = 20
};
QuestionStackLayout.Children.Add(QuestionTextBlock);
}
public Question Question
{
get
{
return question;
}
set
{
question = value;
DisplayQuestion();
}
}
private void DisplayQuestion()
{
QuestionTextBlock.Text = question.QuestionText;
foreach(var answer in question.Answers)
{
AddAnswerButton(answer);
}
}
private void AddAnswer(Answer answer)
{
QuestionStack.Children.Add(CreateButtonForAnswer(answer));
}
private CheckBox CreateCheckBoxForAnswer(Answer answer)
{
var checkBox new CheckBox()
{
Content = answer.Text,
Margin = 10
}
checkBox.Checked += (sender, eventArgs) =>
{
answer.IsSelected = (sender as CheckBox).IsChecked;
};
return checkBox;
}
}
You can now stack instances of QuestionControl. Since the Answer-objects are updated by the CheckBox.Click event you can simply access QuestionControl.Question to get which answers are selected. You might think about deep copying the Question when setting QuestionControl.Question instead of just setting the reference, to avoid side effects.
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 6 years ago.
Improve this question
After PivotItem pivotItem = new PivotItem(); I'm getting Additional information: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD)). What should it be? I'm pretty confused of it.
Code:
foreach (Source source in sources)
{
PivotItem pivotItem = new PivotItem(); /* At this point it falls. */
pivotItem.Header = source.Name;
pivotItem.Margin = new Thickness(0, -10, 0, 0);
ListView listView = new ListView();
listView.ItemsSource = source.Articles;
listView.ItemTemplate = (DataTemplate)Resources["MainItemTemplate"];
listView.ItemClick += OpenArticle_ItemClick;
listView.SelectionMode = ListViewSelectionMode.None;
listView.IsItemClickEnabled = true;
pivotItem.Content = listView;
pvtMain.Items.Add(pivotItem);
}
Based on the exception, you seem to be trying to create a new PivotItem in a thread other than the UI thread. You are only allowed to interact with the UI elements in the UI thread.
You're probably calling this code from an event handler that wasn't triggered from a UI event. You should be able to resolve the issue by using the Dispatcher to switch back to the UI thread:
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
foreach (Source source in sources)
{
PivotItem pivotItem = new PivotItem(); /* At this point it falls. */
pivotItem.Header = source.Name;
pivotItem.Margin = new Thickness(0, -10, 0, 0);
ListView listView = new ListView();
listView.ItemsSource = source.Articles;
listView.ItemTemplate = (DataTemplate)Resources["MainItemTemplate"];
listView.ItemClick += OpenArticle_ItemClick;
listView.SelectionMode = ListViewSelectionMode.None;
listView.IsItemClickEnabled = true;
pivotItem.Content = listView;
pvtMain.Items.Add(pivotItem);
}
});
I am writing an application to Windows Store, it will have exercises with questions which can be answered. I created a class Question where I have 2 variables question and answer.
Example of question:
Question q = new Question(
"This is simple [1] of question. This is the [2]",
new string[]
{
"sample",
"end"
});
What I want is to add questions to a grid view as a TextBlock with a question and a TextBox (the place where we will write the answer) in a place of [1] and [2]. So it would look like this:
<TextBox>
<TextBlock> This is simple </TextBlock>
<TextBox/>
<TextBlock> of question. This is the </TextBlock><TextBox/>
I'm not sure if I'm right to do it to look like that. Can I create a method in the Question class which adds items to MainPage.xaml in the way I presented?
I think you should make your own UserControl to display questions. The point is to create controls (TextBlock/TextBox) dynamically in code behind and add them as children to the WrapPanel. I can't see simple only-XAML decision here.
XAML:
<UserControl <!--skipped--> >
<WrapPanel x:Name="mainPanel">
</WrapPanel>
</UserControl>
Code behind:
partial class QuestionControl : UserControl
{
public QuestionControl()
{
InitializeComponent();
}
private Question _question = null;
public Question Question
{
get { return _question; }
set
{
_question = value;
UpdateUI();
}
}
private void UpdateUI()
{
mainPanel.Children.Clear();
if (this.Question != null)
{
List<FrameworkElement> controls = new List<FrameworkElement>();
string[] questionSegments = // some code to split question here
foreach (var qs in questionSegments)
{
controls.Add(new TextBlock() { Text = qs } );
}
for (int i = 0; i < this.Question.AnswerStrings.Length; i++)
{
string answer = this.Question.AnswerStrings[i];
TextBox newTextBox = new TextBox();
controls.Insert(i * 2 + 1, newTextBox); // inserting TextBoxes between TextBlocks
}
foreach (var control in controls)
{
mainPanel.Children.Add(control); // adding all the controls to the wrap panel
}
}
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is for my button named Edit, when you have an entry into the shopping basket and click on the entry and click Edit it opens up a new window which allows you to edit the entries, product name, quantity or price. This is what I have and it compiles and runs fine but is there an easier way to write it?
private void btn_Edit_Click(object sender, EventArgs e)
{
if (lst_Results.SelectedIndex >= 0)
{
// Want to edit the value of the Item
Edit editbutton = new Edit();
editbutton.NameOfItem =
basket.Items[lst_Results.SelectedIndex].ItemName;
editbutton.Quantity = basket.Items[lst_Results.SelectedIndex].Quantity;
editbutton.ReplacementValue =
basket.Items[lst_Results.SelectedIndex].Price;
if (editbutton.ShowDialog() == DialogResult.OK)
{
basket.UpdateReplacementValue(basket.Items[lst_Results.SelectedIndex].ItemName, editbutton.Quantity, editbutton.ReplacementValue);
RenderLibrary();
}
}
}
At least, you can write out the repeating array access.
// Want to edit the value of the Item
Edit editbutton = new Edit();
var item = basket.Items[lst_Results.SelectedIndex];
editbutton.NameOfItem = item.ItemName;
editbutton.Quantity = item.Quantity;
editbutton.ReplacementValue = item.Price;
if (editbutton.ShowDialog() == DialogResult.OK)
{
basket.UpdateReplacementValue(item.ItemName, editbutton.Quantity, editbutton.ReplacementValue);
RenderLibrary();
}
Also, you might want to just pass the object as parameter to the control, and edit it there using data binding.