So i have 10 TextBlocks named:
x:Name="anlage1" ... x:Name="anlage10"
I want to change at Runtime the BG Color to Red in a loop
Brush red= new SolidColorBrush(Colors.Red);
for(int i=0; i < 11;i++)
{
anlage[i].Background = red;
}
unfortunately the syntax anlage[i] is not working, any solutions for that?
Also this is not working FindName(anlage+i).Background = red;
You're looping 11 times.
Should be 0 to 9 and hence <10 since it's zero indexed.
for(int i=0; i < 10;i++)
{
anlage[i].Background = red;
}
Have you allowed for the first textblock being zero?
I tried this with just 3 textblocks.
You need to cast whatever is found to TextBlock ( or a control so it has a background for sure ).
And they need to have been rendered.
My markup is:
<StackPanel>
<TextBlock Text="0" Name="anlage0"/>
<TextBlock Text="1" Name="anlage1"/>
<TextBlock Text="2" Name="anlage2"/>
</StackPanel>
Code:
private void Window_ContentRendered(object sender, EventArgs e)
{
for (int i = 0; i < 3; i++)
{
((TextBlock)FindName("anlage" + i)).Background = Brushes.Red;
}
}
Which works ok.
Related
I made a custom WPF editor and I am having trouble getting a list of screen control objects (buttons, text boxes, etc) to be displayed in the correct draw order. Here is a visual of my current setup:
I have the control object window working just fine and I can see when the list is changed.
My problem is this: Say that I selected object 3 and pressed the up button. The object should move up in the list (which it does!) BUT the draw order stays the same. Meaning that object 3 should draw behind object 2 and its not doing that. I don't know why.
Here is my code:
private void MoveUp_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < controlObjectList.Count; i++)
{
if (controlObjectList[i].IsSelected)
{
// Copy the current item
var selectedItem = controlObjectList[i];
int newindex = Math.Abs(i - 1);
// Remove the item
controlObjectList.RemoveAt(i);
// Finally add the item at the new index
controlObjectList.Insert(newindex, selectedItem);
}
}
RefreshControlObjectList(controlObjectList);
}
private void MoveDown_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < screenDesigner.m_ParentScreenObject.controlObjects.Count; i++)
{
if (controlObjectList[i].IsSelected)
{
// Copy the current item
var selectedItem = controlObjectList[i];
int newindex = i + 1;
if(newindex < controlObjectList.Count)
{
// Remove the item
controlObjectList.RemoveAt(i);
// Finally add the item at the new index
controlObjectList.Insert(newindex, selectedItem);
}
}
}
RefreshControlObjectList(controlObjectList);
}
private void RefreshControlObjectList(List<ItemsList> newList)
{
newList.Items.Clear();
foreach (ItemsList il in controlObjectList)
{
listObjects.Items.Add(il);
}
//I know this is where I should place the logic for the draw order...
}
#endregion
}
What I'm trying to figure out is how can I refresh the screen in order to see the correct draw order of my objects? Is it possible? Many thanks in advance!
BringForward and SendBackward commands can be accomplished both ways by [1] changing the order of the children or [2] by changing their ZIndex, in fact before the introduction of the ZIndex dependency properties it was only possible by changing the order of the children in the container.
Do you have a complete code sample by any chance? If not here is an example of a sample markup and code which is proving that it is indeed working, but be aware of possible performance impacts of that approach since ZIndex was introduced to fix them.
<Canvas Name="container" MouseDown="OnMouseDown">
<Ellipse Canvas.Left="50" Canvas.Top="10" Fill="Red" Height="100" Width="100"></Ellipse>
<Ellipse Canvas.Left="100" Canvas.Top="20" Fill="Green" Height="100" Width="100"></Ellipse>
<Ellipse Canvas.Left="150" Canvas.Top="30" Fill="Blue" Height="100" Width="100"></Ellipse>
</Canvas>
private void OnMouseDown(object sender, MouseButtonEventArgs e)
{
var uiElement = e.Source as Ellipse;
container.Children.Remove(uiElement);
container.Children.Add(uiElement);
}
You would need to figure out if the list of objects you are manipulating by adding and removing the children is indeed the one. It would be helpful to get some context around the variables controlObjectList and listObjects to see who they belong to.
I am trying to update the list which is Bound to ListBox , When the scroll bar reaches end.I need to update the list and show the changes in UI also.Here it is not updating automatically.Can someone please help me in fulfilling my requirement.
If i tried to use TwoWay mode, It shows below error:
Error : Invalid binding path 'itemsList' : Cannot bind type 'System.Collections.Generic.List(System.String)' to 'System.Object' without a converter
<ScrollViewer
x:Name="sv"
ViewChanged="OnScrollViewerViewChanged">
<ListBox x:Name="listView"
HorizontalAlignment="Left"
Height="Auto"
VerticalAlignment="Top"
Width="172"
ItemsSource="{x:Bind itemsList, Mode=OneWay}"/>
</ScrollViewer>
and the code
public List<String> itemsList = new List<string>();
private void OnScrollViewerViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
var verticalOffset = sv.VerticalOffset;
var maxVerticalOffset = sv.ScrollableHeight; //sv.ExtentHeight - sv.ViewportHeight;
if (maxVerticalOffset < 0 ||
verticalOffset == maxVerticalOffset)
{
// Scrolled to bottom
Util.debugLog("REACHED BOTTOM");
int i;
// itemsList = null;
itemsList.Clear();
for (i = 0; i < 20; i++)
{
itemsList.Add("Item number " + i + 900);
}
}
else
{
// Not scrolled to bottom
// rect.Fill = new SolidColorBrush(Colors.Yellow);
}
}
Here(In below link) is the answer for my question.Thanks alot for all who tried to answer my question.
https://social.technet.microsoft.com/Forums/en-US/7c730558-f933-4483-8d5b-1710d19f99de/xbind-in-windows-10-mode-one-way-i-am-trying-to-update-the-bind-list-when-scrollview-reached-to?forum=wpf
I have to create a grid array the size of this array is determined dynamically.
My try to do this is:
int size = 4; //This "size" will be determined dynamically.suppose i got 4 here
Grid[] rowgrid = new Grid[size];
for (int i = 0; i < size; i++)
{
rowgrid[i].RowDefinitions.Add(new RowDefinition());
}
It don't give any error but when i run it gives exception :
The object reference is not set to an instance of an object.
EDIT:
I want to use array because : after intializing before i have to do like this :
rowgrid[0].Opacity=0.1;
rowgrid[1].Opacity=0.3;
rowgrid[2].Opacity=0.5;
If you suggest me not work programatically then i want to inform in avance that i know that well but i am obliged to do this because i am working in already developed project and no more options are there. It would be a big help if some one bring me to come out of this error or any other alternative to achieve this.
If you want a Grid with differents Rows or Columns. You can use UniformGrid, columns and rows properties are binding.
So,
<UniformGrid Name="uniformGrid1" Rows="{Binding NumberOfRows}" Columns="{Binding NumberOfColumns}">
<Button Content="Button1" Grid.Row="0" Grid.Column="0" />
<Button Content="Button2" Grid.Row="0" Grid.Column="2" />
</UniformGrid>
In your code
private int _numberOfRows;
public int NumberOfRows
{
get { return _numberOfRows; }
set { _numberOfRows= value; RaisePropertyChanged("NumberOfRows"); }
}
private int _numberOfColumns;
public int NumberOfColumns
{
get { return _numberOfColumns; }
set { _numberOfColumns= value; RaisePropertyChanged("NumberOfColumns"); }
}
public MainViewModel()
{
NumberOfColumns = 3;
NumberOfRows = 2;
}
WPF- Selection Brush is not working, I am selecting 'H' in my case but it is not working.
Here is my code :
XAML :
<TextBox Text="Hello" Height="49" Name="textBox2" Width="547" />
C#
textBox2.SelectionStart = 0;
textBox2.SelectionLength = 1;
textBox2.SelectionBrush = Brushes.Red;
Try this
textBox2.Focus();
textBox2.SelectionStart = 0;
textBox2.SelectionLength = 1;
textBox2.SelectionBrush = Brushes.Red;
An alternative solution is to trick the TextBox into thinking it has not lost focus. This way, you're not actually moving focus back to the TextBox.
For this to work, you'd have to set focus on the TextBox at least once, like when the user enters the initial text, or by calling textBox2.Focus() from the constructor.
Markup:
<TextBox Height="49" x:Name="textBox2" LostFocus="TextBox2_OnLostFocus" />
Code-behind:
private void TextBox2_OnLostFocus(object sender, RoutedEventArgs e)
{
e.Handled = true;
}
I have a ListBox which contains a couple of TextBlocks, an Image, and at least 2 TextBoxs. However my problem is that I need to be able to retrieve all the TextBox(s) in the ListBox; APART FROM THE FIRST ONE, and then assign them to a List in C#.
Here is the ListBox in .xaml:
<ListBox Margin="0,-20,0,0" Height="548" Name="listBoxNew">
<TextBlock Name="textBlockName" Text="Name"/>
<TextBox Name="textBoxName" Width="420" Margin="-12,0,0,0"/>
<TextBlock Name="textBlockAdd" Text="Add" Margin="0,10,0,0"/>
<TextBox Name="textBoxAdd" Width="420" Margin="-12,0,0,0"/>
<Image Name="imageAdd" Source="/SecondApp%2b;component/Images/buttonAdd1.png"
Height="50" Margin="0,5,0,0" Tap="imageAdd_Tap"
toolkit:TiltEffect.IsTiltEnabled="True"
ManipulationStarted="imageAddExersize_ManipulationStarted"
ManipulationCompleted="imageAddExersize_ManipulationCompleted" />
</ListBox>
The ListBox may have more TextBoxs than shown in .xaml, as the user can create more by tapping on the Image.
Thank alot, all help is appreciated.
You can do it very simply using Linq. Following sentence returns all the elements from the ListBox of type TextBox except the first one:
var textBoxList = listBoxNew.Items.Where(x => x.GetType() == typeof(TextBox)).Skip(1).ToList();
Remember you have to add using System.Linq; to your file.
hello #Newbie i have solution for you..i know it is not optimized one..but it is working for your case..
i am comparing the types..here ( by not good way) ..i ma doing on a buttonclick..
List<object> lstobj;
private void Button_Click_2(object sender, RoutedEventArgs e)
{
int t = listBoxNew.Items.Count();
lstobj = new List<object>();
TextBox obj = new TextBox();
int p = 0;
for (int i = 0; i < t; i++)
{
if(listBoxNew.Items[i].GetType()==obj.GetType())
{
if (p == 0)
{
p = 1;
continue;
}
else
{
lstobj.Add(listBoxNew.Items[i]);
}
}
}
}
hope it helps you..