Unable to get desired functionality from Radcombobox - wpf

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.

Related

System.InvalidCastException: 'Unable to cast object of type 'System.Windows.Controls.TextBox' to type 'System.IConvertible'.'

I am tring to move the value which is in textbox3 onto a new page but I keep getting an error (see title)
Anybody know how to fix it?
XAML
<TextBox x:Name="textbox1" Width="200" Height="30" Margin="251,50,349,370" Text="{Binding Content, ElementName=label1}"/>
<TextBox x:Name="textbox2" Width="200" Height="30" Margin="0,-292,30,328" Text="10"/>
<TextBox x:Name="textbox3" Width="200" Height="30" Margin="0,-500,45,281" Text="0"/>
CS
double v1;
double v2;
double.TryParse(textbox1.Text, out v1);
double.TryParse(textbox2.Text, out v2);
double v3;
double.TryParse(textbox3.Text, out v3);
textbox3.Text = Convert.ToDouble(v1 * v2).ToString();
TotalHere.TotalNumbers = Convert.ToDouble(textbox3);
Switcher.Switch(new Page2());
You need to reference the text property rather than the textbox itself.
Try changing this:
TotalHere.TotalNumbers = Convert.ToDouble(textbox3);
To:
TotalHere.TotalNumbers = Convert.ToDouble(textbox3.Text);
I would suggest you to use double.TryParse on the Text property of the textBox instead of directly passing it.
Advantage of TryParse method is that you can also check whether the given value is convertible to double or not based on its return value true or false.
Read this
If not then you can also handle that case as well.

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.

whats wrong in Details master with code in devexpress wpf dbgrid ;

I have A dataset and tow Table named t1 and t2 I need Display this Tables in Dbgrid (wpf) as master and Ditalis by Code ;
for more information please see this picture
http://www8.0zz0.com/2014/01/08/07/921573073.jpg
Public Sub New()
InitializeComponent()
Dim Row As DataRow
Dim ds As New DataSet1
Row = ds.T1.NewRow
Row("id") = 1
Row("mname") = "AAA1"
ds.T1.Rows.Add(Row)
Row = ds.T1.NewRow
Row("id") = 2
Row("mname") = "AAA2"
ds.T1.Rows.Add(Row)
Row = ds.T2.NewRow
Row("id") = 1
Row("mname") = "AAA1"
Row("mindex") = 1
ds.T2.Rows.Add(Row)
Row = ds.T2.NewRow
Row("id") = 2
Row("mname") = "AAA2"
Row("mindex") = 1
ds.T2.Rows.Add(Row)
grid1.ItemsSource = ds.DefaultViewManager
End Sub
And this Wpf Tag
<dxg:GridControl x:Name="grid1"
ItemsSource="{Binding t1}"
AutoPopulateColumns="True">
<dxg:GridControl.DetailDescriptor>
<dxg:DataControlDetailDescriptor ItemsSourceBinding="{Binding t2}">
<dxg:GridControl
ImplyNullLikeEmptyStringWhenFiltering="False"
SupportDomainDataSource="False"/>
</dxg:DataControlDetailDescriptor>
</dxg:GridControl.DetailDescriptor>
</dxg:GridControl>
My problem I dont see any data in dbgrid or how I can display master and Ditales data as this way from my dataSet by code
<Grid>
<dxg:GridControl x:Name="grid1"
ItemsSource="{Binding Path=t1}"
AutoPopulateColumns="True"
>
<dxg:GridControl.DetailDescriptor>
<dxg:DataControlDetailDescriptor ItemsSourceBinding="{Binding Path= FK_t1_t2}" >
<dxg:GridControl AutoPopulateColumns="True" >
</dxg:GridControl>
</dxg:DataControlDetailDescriptor>
</dxg:GridControl.DetailDescriptor>
<dxg:GridControl.View>
<dxg:TableView NewItemRowPosition="Bottom"/>
</dxg:GridControl.View>
</dxg:GridControl>
</Grid>

Bind a WPF's control to an F# Option

Suppose I have a business object (without an AllowNullLiteralAttribute).
type Person(name: string) =
member val Name = name
override x.ToString() = name
And a view model, with the selected person optionally set.
type MainWindowModel() =
let mutable selectedPerson: Person option = None
:
member val People = ObservableCollection<Person>()
member x.SelectedPerson
with get() = selectedPerson
and set(v) =
if selectedPerson <> v then
selectedPerson <- v
x.RaisePropertyChanged("SelectedPerson")
What is the best way to bind a WPF control's SelectedItem property to an F# option property (without using AllowNullLiteralAttribute)?
If I do this...
<StackPanel>
<ListBox ItemsSource="{Binding People}"
SelectedItem="{Binding SelectedPerson}"
DisplayMemberPath="Name" />
<TextBlock Text="{Binding SelectedPerson}" />
</StackPanel>
...it results in an error, Cannot convert 'George' from type 'Person' to type 'Microsoft.FSharp.Core.FSharpOption`1[Person]'
The approach I am currently taking is to write my own IValueConverter.
open System
open System.Globalization
open System.Windows.Data
type OptionsTypeConverter() =
// from http://stackoverflow.com/questions/6289761
let (|SomeObj|_|) =
let ty = typedefof<option<_>>
fun (a:obj) ->
let aty = a.GetType()
let v = aty.GetProperty("Value")
if aty.IsGenericType && aty.GetGenericTypeDefinition() = ty then
if a = null then None
else Some(v.GetValue(a, [| |]))
else None
interface IValueConverter with
member x.Convert(value: obj, targetType: Type, parameter: obj, culture: CultureInfo) =
match value with
| null -> null
| SomeObj(v) -> v
| _ -> value
member x.ConvertBack(value: obj, targetType: Type, parameter: obj, culture: CultureInfo) =
match value with
| null -> None :> obj
| x -> Activator.CreateInstance(targetType, [| x |])
And then my XAML looks like this:
<StackPanel>
<ListBox ItemsSource="{Binding People}"
SelectedItem="{Binding SelectedPerson, Converter={StaticResource OptionsTypeConverter1}}"
DisplayMemberPath="Name" />
<TextBlock Text="{Binding SelectedPerson, Converter={StaticResource OptionsTypeConverter1}}" />
</StackPanel>
There may be a simpler way. This converter may already exist in the framework. There may be better implementations out there.
I think using an IValueConverter is probably the cleanest approach.
Alternatively, you could use the fact that option type has Value member (which returns the value for Some or throws an exception for None). So, if you know that there is always a value, or if the exception does not break anything (I have not tried), you might be able to write:
<ListBox ItemsSource="{Binding People}"
SelectedItem="{Binding SelectedPerson.Value}"
DisplayMemberPath="Name" />
If the exception is a problem, then this past SO question suggests to use PriorityBinding:
<ListBox ItemsSource="{Binding People}" DisplayMemberPath="Name">
<ListBox.SelectedItem>
<PriorityBinding>
<Binding Path="SelectedPerson.Value" />
<Binding Source="{x:Null}" /> <!-- or some other default... -->
</PriorityBinding>
</ListBox.SelectedItem>
</ListBox>

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.

Resources