why when use listView some controls in page disappeared? - c#

I use SearchBar to search from listView and I have other controls
but when page load just SearchBar appear and other controls disappear
like this image
and when search then select from listView the disappeared controls appears
like this image
my xaml code :
<StackLayout>
<SearchBar x:Name="search_trade"
TextChanged="search_trade_TextChanged"
/>
<ListView x:Name="list" ItemSelected="list_ItemSelected" >
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Name}" ></TextCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Editor x:Name="edit_trade"
FontSize="14"/>
</StackLayout>
my c# code :
private void search_trade_TextChanged(object sender, TextChangedEventArgs e)
{
if (search_trade.Text.Trim()==string.Empty)
{
list.IsVisible = false;
}
list.IsVisible = true;
if (string.IsNullOrEmpty(e.NewTextValue))
{
list.ItemsSource = tempdata;
}
else
{
list.ItemsSource = tempdata.Where(x => x.Name.ToLower().StartsWith(e.NewTextValue));
}
}
private void list_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var item = (Contacts)e.SelectedItem;
edit_trade.Text = item.Name.ToString();
search_trade.Text = "";
list.IsVisible = false;
edit_trade.IsReadOnly = true;
}
code c# that I initially set the ItemsSource :
public Anti_biotic()
{
InitializeComponent();
active();
years_months();
frame_view.IsVisible = false;
trade();
}
private void trade()
{
tempdata = new List<Contacts>
{
new Contacts() { Name = "Amoclawin228.5mg/5mlsusp60ml"},
new Contacts() { Name = "Amoclawin 457mg / 5ml susp 60 ml"},
new Contacts() { Name = "Amoflux 250 mg susp 100 ml"},
};
}
how can I solve this issue?

you are not assigning the ListView's ItemsSource when the page loads. If the ListView doesn't have any data then it won't display anything
public Anti_biotic()
{
InitializeComponent();
active();
years_months();
frame_view.IsVisible = false;
// this method creates tempdata but doesn't do anything with it
trade();
// add this
list.ItemsSource = tempdata;
}

Related

How to define my own filter while working with Telerik RadGridView control?

I'm working on a C# application, where I'm trying to create my own filtering mechanism for filtering a Telerik RadGridView control.
My RadGridView control looks as follows (in the xaml file, I've left some open lines for showing the part where I define my filter):
<telerik:RadGridView x:Name="PartsGridView"
AutoGenerateColumns="False"
CanUserFreezeColumns="False"
FilteringMode="Popup"
FilterOperatorsLoading="Grid_FilterOperatorsLoading"
ItemsSource="{Binding PagedSource, ElementName=PartsRadDataPager}"
IsSynchronizedWithCurrentItem="False"
SelectedItem="{Binding SelectedPart}"
SelectionMode="Single"
ShowInsertRow="False"
EnableRowVirtualization="True"
RowLoaded="PartsGridView_RowLoaded"
FieldFilterEditorCreated="FieldFilterEditorCreated"
IsFilteringAllowed="True"
DistinctValuesLoading="PartsGridView_DistinctValuesLoading"
Grouped="PartsGridView_Grouped">
<telerik:RadGridView.GroupDescriptors>
<telerik:GroupDescriptor Member="Name" DisplayContent="Article"/>
</telerik:RadGridView.GroupDescriptors>
<telerik:RadGridView.FilterDescriptors/>
<telerik:RadGridView.Columns>
...
<telerik:GridViewDataColumn DataMemberBinding="{Binding Article.CustomerReference}"
Header="Client"
IsReadOnly="True"
Name="Client">
<telerik:GridViewDataColumn.FilteringControl>
<local:Myfilter Column="{Binding ElementName=Client}"/>
</telerik:GridViewDataColumn.FilteringControl>
</telerik:GridViewDataColumn>
</telerik:RadGridView.Columns>
</telerik:RadGridView>
The corresponding source code is as follows (I've only shown the OnFilter, it's the idea to extend the filtering possibilities to regular expressions, but let's start making the regular filter work first):
public partial class MyFilter : System.Windows.Controls.UserControl,
IFilteringControl
{
private GridViewDataColumn column;
private CompositeFilterDescriptor compositeFilter;
private Telerik.Windows.Data.FilterDescriptor textFilter;
private void OnFilter(object sender, RoutedEventArgs e)
{
//var reg = new Regex(textBox.Text.Replace("*", ".*"));
// var descriptor = new FilterDescriptor<string>
// { FilteringExpression = o => reg.IsMatch(o) };
compositeFilter = new CompositeFilterDescriptor();
textFilter = new Telerik.Windows.Data.FilterDescriptor(Column.Name
, Telerik.Windows.Data.FilterOperator.IsEqualTo
, null);
compositeFilter.FilterDescriptors.Add(textFilter);
textFilter.Value = TextBox;
if (!Column.DataControl.FilterDescriptors.Contains(compositeFilter))
{
Column.DataControl.FilterDescriptors.Add(compositeFilter);
}
IsActive = true;
}
When debugging, I go into the OnFilter() function but instead of just filtering, I don't see any data in the RadGridView anymore.
Does anybody have an idea what I'm doing wrong?
Edit: In case the question is not clear, don't hesitate asking for more information.
Try this:
private void OnFilter(object sender, RoutedEventArgs e)
{
//var reg = new Regex(textBox.Text.Replace("*", ".*"));
// var descriptor = new FilterDescriptor<string>
// { FilteringExpression = o => reg.IsMatch(o) };
//compositeFilter = new CompositeFilterDescriptor();
textFilter = new Telerik.Windows.Data.FilterDescriptor(Column.Name
, Telerik.Windows.Data.FilterOperator.IsEqualTo
, null);
//compositeFilter.FilterDescriptors.Add(textFilter);
textFilter.Value = TextBox;
if (!Column.DataControl.FilterDescriptors.Contains(textFilter))
{
Column.DataControl.FilterDescriptors.Add(textFilter);
}
IsActive = true;
}
u can also try this:
private void OnFilter(object sender, RoutedEventArgs e)
{
compositeFilter = new CompositeFilterDescriptor();
textFilter = new Telerik.Windows.Data.FilterDescriptor(Column.Name, Telerik.Windows.Data.FilterOperator.IsEqualTo, null);
compositeFilter.FilterDescriptors.Add(textFilter);
textFilter.Value = TextBox;
if (!Column.DataControl.FilterDescriptors.Contains(compositeFilter))
{
Column.DataControl.FilterDescriptors.Add(compositeFilter);
}
IsActive = true;
}

Enable `CanUserAddRows` with dynamically created `DataGrid` columns - WPF

The DataGrid columns are dynamically created in code behind. In order to bind the columns dynamically I have a Dictionary<string, obj> property, with the key as the column header, and the values.
and the columns gets bound to the dictionary like this:
var item = new DataGridTextColumn();
item.Width = new DataGridLength(1, DataGridLengthUnitType.Auto);
item.Header = name;
item.Binding = new Binding($"di[{name}].obj") { Mode = BindingMode.TwoWay };
It works fine, but I couldn't figure out how to get the values from the new row form DataGrid
I've created a test project and ExpandoObject worked for me. I was able to display dynamic column "FirstName" and also add new rows by editing grid rows
XAML:
<Grid Margin="0,0,0,56" Loaded="Grid_Loaded_1">
<DataGrid Name="MyGrid" ItemsSource="{Binding Items}" HorizontalAlignment="Left" Margin="69,33,0,0" VerticalAlignment="Top" Height="220" Width="389"/>
<Button Content="Button" HorizontalAlignment="Left" Height="22" Margin="339,286,0,-45" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</Grid>
CS:
public partial class MainWindow : Window
{
public ViewModel Vm { get; set; }
public MainWindow()
{
InitializeComponent();
}
private void Grid_Loaded_1(object sender, RoutedEventArgs e)
{
Vm = new ViewModel();
Vm.Items = new ObservableCollection<ExpandoObject>();
DataContext = Vm;
var fieldName = "FirstName";
var item = new ExpandoObject() as IDictionary<string, Object>;
item.Add(fieldName, "Adam"); // Dynamically adding new fields
var eoItem = item as ExpandoObject;
Vm.Items.Add(eoItem);
// Dynamically adding new columns
var col = new DataGridTextColumn();
col.Width = new DataGridLength(1, DataGridLengthUnitType.Auto);
col.Header = fieldName;
col.Binding = new Binding(fieldName) { Mode = BindingMode.TwoWay };
MyGrid.Columns.Add(col);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
}
}
public class ViewModel
{
public ObservableCollection<ExpandoObject> Items { get; set; }
}
I had similar problem some time ago. Below You can find my solution. Basically I needed to change AddingNewRow event. You will need to change types etc. to meet your needs but this should be rather easy.
private void AddingNewRow(object sender, AddingNewItemEventArgs e)
{
DictionaryRowData newRow = new DictionaryRowData();
for (int i = 0; i < dictionaryData.Columns.Count; i++)
{
newRow.Cells.Add(new DictionaryCellData());
}
e.NewItem = newRow;
}
Sorry, for not matching exactly your case, but I don't have time right now. I hope it will help

Change image of a button that is on an element of a LongListSelector

I have this longListSelector:
<phone:LongListSelector
x:Name="ListaMensajesTablon"
ItemsSource="{Binding Mensajes}"
ItemTemplate="{StaticResource MensajesTablonDataTemplate}"
SelectionChanged="MensajeTablonSelected"/>
With this ItemTemplate:
<DataTemplate x:Key="MensajesTablonDataTemplate">
<Grid>
<Button MaxHeight="85" MaxWidth="95" MinHeight="85" MinWidth="95" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center" Click="Button_Click" BorderBrush="Transparent">
<Button.Content>
<Image x:Name="imagenFav" MaxHeight="75" MaxWidth="75" MinHeight="75" MinWidth="75"
Source="{Binding userFav, Converter={StaticResource BoolToHeart}}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Button.Content>
</Button>
</Grid>
</DataTemplate>
This code-behind:
private void Button_Click(object sender, RoutedEventArgs e)
{
botonFavPulsado = true;
botonAmor = (Button)sender;
}
private void MensajeTablonSelected(object sender, SelectionChangedEventArgs e)
{
if(botonFavPulsado)
{
var myItem = ((LongListSelector)sender).SelectedItem as MensajeTablon;
if(botonAmor!=null)
{
if (myItem.userFav)
{
botonAmor.Content = new Image
{
Source = new BitmapImage(new Uri("icons/heart.red.png", UriKind.Relative))
};
}
else
{
botonAmor.Content = new Image
{
Source = new BitmapImage(new Uri("icons/heart.white.png", UriKind.Relative))
};
}
}
botonFavPulsado = false;
}
}
I want to do is that when you press a button that is inside an element of LongListSelector change the picture . The first time I press the button enters in the function Button_Click and then enters in the function MensajeTablonSelected function and change the image (good). The problem is the second time I press the same button is entering in the function Button_Click function and does not enter in the function MensajeTablonSelected
Resume : ToggleButton in LongItemSelector working the first time but not the second one
Problem solved:
private void MensajeTablonSelected(object sender, SelectionChangedEventArgs e)
{
if (((LongListSelector)sender).SelectedItem != null)
if(botonFavPulsado)
{
var myItem = ((LongListSelector)sender).SelectedItem as MensajeTablon;
if(botonAmor!=null)
{
if (myItem.userFav)
{
botonAmor.Content = new Image
{
Source = new BitmapImage(new Uri("icons/heart.red.png", UriKind.Relative))
};
}
else
{
botonAmor.Content = new Image
{
Source = new BitmapImage(new Uri("icons/heart.white.png", UriKind.Relative))
};
}
}
botonFavPulsado = false;
//Unselect ITEM
((LongListSelector)sender).SelectedItem = null;
}
}
This solution have a problem, the function MensajeTablonSelected is called again.

How can I send 'ListBox selected item`s value' to LocalDatabase - Windows Phone

I have a listbox like this:
<toolkit:ListPicker Name="lstBoxBaseUnitOfMeasure" Width="100" Margin="0,4,0,0">
<toolkit:ListPicker.Items>
<TextBlock Text="EACH" Height="30"/>
<TextBlock Text="GRAM" Height="30"/>
</toolkit:ListPicker.Items>
</toolkit:ListPicker>
I would like to send selected item to Local Database like this:
private void AddProduct_Click(object sender, RoutedEventArgs e)
{
TblProductsToOrder newProductToOrder = new TblProductsToOrder
{
OrderNId = selectedID,
Quantity = int.Parse(txtQuantity.Text),
**BaseUnitOfMeasure = ??????????????**
};
}
ListPicker fires an event,SelectionChanged whenever a item is selected. You will need to listen to that event
<toolkit:ListPicker Name="lstBoxBaseUnitOfMeasure" Width="100" Margin="0,4,0,0" SelectionChanged="listPicker_SelectionChanged">
<toolkit:ListPicker.Items>
<TextBlock Text="EACH" Height="30"/>
<TextBlock Text="GRAM" Height="30"/>
</toolkit:ListPicker.Items>
</toolkit:ListPicker>
private void listPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (lstBoxBaseUnitOfMeasure.SelectedItem != null)
{
var texBlock = (TextBlock) lstBoxBaseUnitOfMeasure.SelectedItem;
selectedUnit = texBlock.Text;
TblProductsToOrder newProductToOrder = new TblProductsToOrder
{
OrderNId = selectedID,
Quantity = int.Parse(txtQuantity.Text),
BaseUnitOfMeasure = selectedUnit
};
}
}
Assuming that BaseUnitOfMeasure property is of type String, you can try this way :
String selectedUnit = "";
if(lstBoxBaseUnitOfMeasure.SelectedItem != null)
{
var selectedTextBlock = (TextBlock)lstBoxBaseUnitOfMeasure.SelectedItem;
selectedUnit = selectedTextBlock.Text;
}
TblProductsToOrder newProductToOrder = new TblProductsToOrder
{
OrderNId = selectedID,
Quantity = int.Parse(txtQuantity.Text),
BaseUnitOfMeasure = selectedUnit
};

Silverlight Master/Detail's Situation

I have a DataGrid. The ItemsSource of this DataGrid is set in the Completed event of a WCF call.However, when adding a Detail Datagrid inside the master grids DataTemplate and naming it appropriately... Ineed to fill it master grids selection change event but my codebehind does not recognise the detail grid. I cant set the ItemsSource of grdDetail like I do with grdMaster.So how i can fill my detail datagrid ?
Xaml File
<Grid x:Name="LayoutRoot">
<sdk:DataGrid x:Name="dgCustList" AutoGenerateColumns="False" Background="Transparent" SelectionChanged="dgCustList_SelectionChanged">
<sdk:DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel x:Name="stkPanel">
<sdk:DataGrid x:Name="dgCustDetail" RowDetailsVisibilityMode="VisibleWhenSelected" AutoGenerateColumns="False" Background="Transparent"/>
</StackPanel>
</DataTemplate>
</sdk:DataGrid.RowDetailsTemplate>
</sdk:DataGrid>
<Grid.Projection>
<PlaneProjection x:Name="Projection"/>
</Grid.Projection>
</Grid>
And CodeBehind
public MusteriListe()
{
InitializeComponent();
var stb1 = new Storyboard { Duration = new Duration(TimeSpan.FromSeconds(1)), SpeedRatio = 3 };
var daY1 = new DoubleAnimation { From = 0.00, To = 90.00 };
Storyboard.SetTargetName(daY1, "Projection");
Storyboard.SetTargetProperty(daY1, new PropertyPath("RotationX"));
stb1.Children.Add(daY1);
this.Resources.Add("EndOfPage", stb1);
var stb = new Storyboard();
stb.Duration = new Duration(TimeSpan.FromSeconds(1));
stb.SpeedRatio = 3;
var daY = new DoubleAnimation { From = -90.00, To = 0.00 };
Storyboard.SetTargetName(daY, "Projection");
Storyboard.SetTargetProperty(daY, new PropertyPath("RotationX"));
stb.Children.Add(daY);
Resources.Add("StartOfPage", stb);
dgCustList.Columns.Add(new DataGridTextColumn
{
Header = "ID",
Binding = new Binding("CustomerID")
});
dgCustList.Columns.Add(new DataGridTextColumn
{
Header = "Müşteri Ad",
Binding = new Binding("CustomerName")
});
dgCustList.Columns.Add(new DataGridTextColumn
{
Header = "Müşteri Soyad",
Binding = new Binding("CustomerSurname")
});
dgCustList.Columns.Add(new DataGridTextColumn
{
Header = "Müşteri Tel",
Binding = new Binding("CustomerPhone")
});
LoadGrid();
}
private void LoadGrid()
{
var client = new EczServiceClient();
client.CustomerInfoCompleted += client_CustomerInfoCompleted;
client.CustomerInfoAsync();
}
void client_CustomerInfoCompleted(object sender, CustomerInfoCompletedEventArgs e)
{
dgCustList.ItemsSource = e.Result;
}
private void dgCustList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var customer = dgCustList.SelectedItem as CustomerInfo;
if (customer == null) return;
var client = new EczServiceClient();
client.CustomerDetailCompleted += client_CustomerDetailCompleted;
client.CustomerDetailAsync(customer.CustomerID);
}
void client_CustomerDetailCompleted(object sender, CustomerDetailCompletedEventArgs e)
{
IN HERE I WANT TO FILL DATAGRID LIKE MASTER GRID BUT ITS NOT LET ME ( dgCustDetail.ItemSource = e.Result)
}
You did NOTdefine binding for you detail data grid and you set the autogenerate columns to false. You need to define the binding either in XAML or in code behind before the client_CustomerDetailCompleted event fires. Simply setting the item source alone won't work because the detail datagrid does not contain columns. This is where MVVM pattern comes in handy.

Categories