If element name is not given in Combobox Itemsource binding, then it display empty combobox - silverlight

This is the code snippet:
setter.Value = (DataTemplate)System.Windows.Markup.XamlReader.Load(
#"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
<StackPanel HorizontalAlignment='Left' Height='Auto' VerticalAlignment='Top' Width='Auto'>
<Grid Width='Auto'> <Grid.ColumnDefinitions> <ColumnDefinition Width='80*'/>
<ColumnDefinition Width='20*'/></Grid.ColumnDefinitions>
<Grid.Resources><Style TargetType='ComboBoxItem' x:Key='comboboxStyle' >
<Setter Property='FlowDirection' Value='LeftToRight'/>
<Setter Property='Width' Value='" + ColumnWidthList[i] + "' /></Style></Grid.Resources>
<TextBlock Grid.Column='0' Text='" + columnName + "' HorizontalAlignment='Left'/>
<ComboBox MinWidth='16' Grid.Column='1' FlowDirection='RightToLeft' ItemContainerStyle='{StaticResource comboboxStyle}' Name='cmbColors' ItemsSource='{Binding NewLst[" + i + "]}' SelectedItem='{Binding SelectedIt, Mode=TwoWay,ElementName=s1}' SelectedValue='{Binding SelectedValue, Mode=TwoWay,ElementName=s1}' SelectedValuePath='Item1' DisplayMemberPath='Item1'> </ComboBox></Grid></StackPanel></DataTemplate>");
headerStyle.Setters.Add(setter);
datagrid1.Columns[i].HeaderStyle = headerStyle;
If I give ,ElementName=s1 in this then it works fine
ItemsSource='{Binding NewLst[" + i + "],ElementName=s1}
Can anyone please suggest how we could use this without the element name?
Element Name is the name of the user control x:name="s1".
As I'm using this code in templated control with the Generic.xaml, there is no user control. So I want to use it without the element name. I'm creating combobox dynamically to display it in the column header of DataGrid

Related

Find the index of an item in ComboBox

How can I find value index from ComboBox? I tried this but it always returns -1;
sexCombo.SelectedIndex = sexCombo.Items.IndexOf(teacherInfo["sex"].ToString());
Here how the ComboBox items are added:
<ComboBox x:Name="sexCombo" Margin="5,20,10,0" VerticalAlignment="Top" Width="100" Style="{StaticResource MaterialDesignFloatingHintComboBox}" materialDesign:HintAssist.Hint="الجنس" HorizontalContentAlignment="Left" Height="45" VerticalContentAlignment="Bottom">
<ComboBoxItem Content="ذكر"/>
<ComboBoxItem Content="أنثى"/>
</ComboBox>
The Items collection of the ComboBox contains ComboBoxItems so you need to get the index of the corresponding ComboBoxItem element. Try this:
var comboBoxItem = sexCombo.Items.OfType<ComboBoxItem>().FirstOrDefault(x => x.Content.ToString() == teacherInfo["sex"].ToString());
int index = sexCombo.SelectedIndex = sexCombo.Items.IndexOf(comboBoxItem);
To use combobox.items.indexof, you need to put String in combobox, like this:
<ComboBox x:Name="Combobox1" HorizontalAlignment="Left" Margin="504,8,0,0" VerticalAlignment="Top" Width="120" Height="25">
<System:String>Item1</System:String>
<System:String>Item2</System:String>
<System:String>Item3</System:String>
</ComboBox>
Then when you use Combobox1.items.indexof("Item2") it will return 1.
I had the same problem. I solved it like this.
For i = 0 To comboBox.Items.Count - 1
If comboBox.Items(i).ToString = "searchString" Then
comboBox.SelectedIndex = i
Exit For
End If
Next i
This will make the selection of the string value you're searching for.

Unable to get desired functionality from Radcombobox

I wish to display a list of images in a cbo dropdown, show the name of the image in the textbox of the cbo, and store the path to the image back to the database.
The following code binds the cbo:
Dim logoImages As List(Of Logos) = New List(Of Logos)
Dim theLogoName As String = String.Empty
Dim theLogoPath As String = String.Empty
thisCombo.ItemsSource = Nothing
Dim listOfImages() As String = Directory.GetFiles("C:\XXX\logo", "*.*")
For i As Integer = 0 To listOfImages.Count - 1
theLogoPath = New Uri(listOfImages(i)).AbsolutePath
theLogoName = Mid(theLogoPath, 13)
logoImages.Add(New Logos() With {.logoImage = New Uri(listOfImages(i)), .logoName = theLogoName, .logoPath = theLogoPath})
Next
thisCombo.ItemsSource = logoImages
XAML:
<telerik:RadComboBox x:Name="cboLogo" FontSize="16" Background="#F6F8FA" BorderBrush="#D7D8DD"
SelectedValue="{Binding logoPath, Mode=TwoWay}"
Text="{Binding logoName}"
IsEditable="True" IsReadOnly="True" TabIndex="9"
Style="{DynamicResource RadComboBoxStyle3}" >
<telerik:RadComboBox.ItemTemplate>
<DataTemplate>
<WrapPanel Margin="0 5 0 5" Height="70">
<Image Height="65" Stretch="Fill" Source="{Binding logoImage}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</WrapPanel>
</DataTemplate>
</telerik:RadComboBox.ItemTemplate>
</telerik:RadComboBox>
The dropdown displays the images correctly. I cannot get the name of the image to display in the text part of the cbo and cannot get the path back to the database.
Can someone show me what I'm doing wrong? Thanks
This is what was needed to display the image name. Idea from here and here:
TextSearch.TextPath="logoName"
Using the SelectedValue to the database was what was needed to store the path back to the database.

How to change Text of TextBlock which is in DataTemplate of Row Details for each DataGrid Row Details?

I have Datagrid which is clicking by mouse in each row is showing data grid row details. here is code,
Microsoft.Windows.Controls.DataGridRow row = (Microsoft.Windows.Controls.DataGridRow)(DataGrid1.ItemContainerGenerator.ContainerFromItem(DataGrid1.SelectedItem));
DataGridDetailsPresenter presenter = FindVisualChild<DataGridDetailsPresenter>(row);
DataTemplate template = presenter.ContentTemplate;
TextBlock txt = (TextBlock)template.FindName("rowdetails", presenter);
txt.Text = retString;
And also I have Checkbox, when you check it, it should show all row details.
I am trying this code for showing all rowdetails
if ((bool)chkboxRowDetails.IsChecked)
{
DataGrid1.RowDetailsVisibilityMode = Microsoft.Windows.Controls.DataGridRowDetailsVisibilityMode.Visible;
for (int i = 0; i < DataGrid1.Items.Count-1; i++)
{
Microsoft.Windows.Controls.DataGridRow row = (Microsoft.Windows.Controls.DataGridRow)(DataGrid1.ItemContainerGenerator.ContainerFromIndex(i));
DataGridDetailsPresenter presenter = FindVisualChild<DataGridDetailsPresenter>(row);
DataTemplate template =presenter.ContentTemplate;
TextBlock txt = (TextBlock)template.FindName("rowdetails", presenter);
txt.Text = retString;
}
But it is giving error. "This operation is valid only on elements that have this template applied."
Showing in line TextBlock txt = (TextBlock)template.FindName("rowdetails", presenter);
Do you have any idea what is wrong in my code. I want to show all row details by checking checkbox. My Data template is here
<WpfToolkit:DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel HorizontalAlignment="Stretch" Orientation="Vertical" Margin="5">
<TextBlock Foreground="CadetBlue" FontSize="14"
TextWrapping="Wrap" Name="rowdetails" HorizontalAlignment="Stretch"
/>
</StackPanel>
</DataTemplate>
</WpfToolkit:DataGrid.RowDetailsTemplate>
I solved this problem by adding some codes in row details load. Here is code.
TextBlock txt1 = e.DetailsElement.FindName("rowdetails") as TextBlock;
txt1.Text = retString; // where retString is variable string.

Dynamically adding and moving ellipses using WPF

I'm using WPF and having trouble dynamically/programatically adding ellipses to my grid.
I'm dynamically allocating and placing ellipses inside myGrid. Trouble is the position on the ellipses don't change. I'm using Canvas.SetLeft and SetTop, but the ellipses still seem stuck.
Here is the code for dynamic allocation :
{
...
Ellipse el = new Ellipse();
RadialGradientBrush b = new RadialGradientBrush();
b.RadiusX = r * 10.0f;
b.RadiusY = r * 10.0f;
b.GradientOrigin = new Point(0.5f, 0.5f);
b.GradientOrigin = new Point(0.5f, 0.5f);
b.GradientStops.Add(new GradientStop(Colors.Green, 0.0));
b.GradientStops.Add(new GradientStop(Colors.Blue, 1.0));
el.Width = 5.0f + r * 20.0f;
el.Height = 5.0f + r * 20.0f;
el.Stroke = b;
SetEllipsePosition(el, p);
this.myGrid.Children.Add(el);
...
}
private void SetEllipsePosition(FrameworkElement ellipse, Point j)
{
Canvas.SetLeft(ellipse, j.X);
Canvas.SetTop(ellipse, j.Y);
}
<Grid Height="480" Name="myGrid" Width="640">
<GroupBox Header="Pattern" Height="117" HorizontalAlignment="Left" Margin="10,564,0,0" Name="groupBox1" VerticalAlignment="Top" Width="238"></GroupBox>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="33,30,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click_1" />
<Grid Margin="6,507,408,-121">
<RadioButton Content="Lines" Height="16" HorizontalAlignment="Left" Margin="15,18,0,0" Name="rbLines" VerticalAlignment="Top" GroupName="RenderStyles" />
<RadioButton Content="Circles" Height="16" HorizontalAlignment="Left" Margin="15,49,0,0" Name="rbCircles" VerticalAlignment="Top" GroupName="RenderStyles" />
</Grid>
</Grid>
The problem is that you are using a Grid but setting Canvas properties, you could add a Canvas into the Grid and draw you ellipses on the Canvas ( add them to the Canvases children), and then it would work.
Or you could use the Margin property of your ellipse to set it's position on the Grid
Canvas.Left and Canvas.Top are attached properties: you set them only if your UI element is going to be contained in a Canvas; and only when it is on a Canvas will those properties be used (by the Canvas layout manager). Same with attached properties from Grid (such as Grid.Column to tell the Grid parent in what column the UI elements "wants" to be), Panel (Panel.ZIndex to tell the Panel parent at what z index the UI element should be put), etc.

Silverlight Canvas on Scrollviewer not triggering

Why does this work so well in wpf
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<Canvas x:Name="MyDesigner">
</Canvas>
</ScrollViewer>
Now when I do the same thing in silverlight and load an control "that can be dragged" the scrollbars are not triggered, when I drag out of view, nothing happens... but in wpf it automatically shows them...
As a quick check against the normal 'gotcha' have you explicitly set the canvas height / width properties?
If I knock up some xaml for test purposes and run it:
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<Canvas x:Name="test" Background="Beige">
<TextBlock Canvas.Left="2000" Canvas.Top="200" Text="test"/>
</Canvas>
</ScrollViewer>
Will not show a scroll bar, even though I have explicitly created content in the canvas 2000 to the left, the canvas width not being set means the scroll viewer has no range to bind to so to speak. The canvas without a width is considered is just infinitely wide from what I can see. Whilst this is not the same as dragging the concept of putting a piece of content outside of the current view is there.
As soon as you add a width, it defines a finite area to scroll on and the scroll bar shows up.
Here is the solution I found.
The canvas can grow dynamically, but you will need to explicitly set the height to a new value.
So, if you have 20 textblocks with a height of 21, you need to set the canvas height as:
Canvas.Height = 22.0 * 100;
for the scrollviewer to pick up the new height.
MainPage.xaml
<Canvas Canvas.Left="5" Canvas.Top="25" >
<ScrollViewer Width="300" Height="700" x:Name="CanSummaryScroller">
<Canvas x:Name="canSummaryCells" >
</Canvas>
</ScrollViewer>
</Canvas>
MainPage.xaml.cs
boxDevSumList is a List of TextBoxes
for (int i = 0; i < 100; i++)
{
boxDevSumList.Add(new Cell());
(boxDevSumList.ElementAt(i)).Width = 271;
(boxDevSumList.ElementAt(i)).Height = 21;
(boxDevSumList.ElementAt(i)).SetValue(FontFamilyProperty, new FontFamily("Calibri"));
(boxDevSumList.ElementAt(i)).FontSize = 12;
(boxDevSumList.ElementAt(i)).Text = "this is a test";
if (i.Equals(1) || i.Equals(3) || i.Equals(5) || i.Equals(7) || i.Equals(9))
(boxDevSumList.ElementAt(i)).Background = lgBrush;
(boxDevSumList.ElementAt(i)).Opacity = .9;
canSummaryCells.Children.Add(boxDevSumList.ElementAt(i));
boxDevSumList.ElementAt(i).SetValue(Canvas.TopProperty, (double)(i * 21) + 45);
boxDevSumList.ElementAt(i).SetValue(Canvas.LeftProperty, 4.0);
canSummaryCells.Height = 22.0 * 100;
}

Resources