Disabling button on a particular row in WPF gridview - c#

This is my code for generating the gridview
User u1 = new User();
User u2 = new User();
for (int i = 0; i < files.Count; i++)
{
u1.Files = files[i];
items.Add(u1);
}
for (int i = 0; i < sharedFiles.Count; i++)
{
u2.Files += sharedFiles[i];
items.Add(u2);
}
gridview1.ItemsSource = items;
}
public class User
{
public string Files { get; set; }
}
This is my xaml page
<ListView.View>
<GridView >
<GridView.Columns>
<GridViewColumn Width="250" DisplayMemberBinding="{Binding Files}" ></GridViewColumn>
<GridViewColumn >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="Download" Click="fileDownloadClick" CommandParameter="{Binding Files}"></Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="Share" Click="fileShareClick" CommandParameter="{Binding Files}" ></Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
I would like to know if its possible to only allow my Share buttons disabled in my 2nd for loop (u2) while my Share buttons in (u1) is still enabled. Currently both are enabled. I am currently using this to retrieve values for particular role
private void fileShareClick(object sender, RoutedEventArgs e)
{
object share = ((Button)sender).CommandParameter;
}

Create a DataTrigger on the Button which you want. and bind that with property that represent the boolen type value. like IsShareAllowed.
Try some thing like this.
var procs = (from p in System.Diagnostics.Process.GetProcesses()
select new
{
Name = p.ProcessName,
IsShareAllowed = Convert.ToBoolean(r.Next(-1, 1))
}).ToList(); ;
list.ItemsSource = procs;
Xaml
<ListView Name="list" Grid.Row="4">
<ListView.View>
<GridView >
<GridView.Columns>
<GridViewColumn Width="250" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="Download" Click="fileDownloadClick" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="Share" Click="fileShareClick" >
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding IsShareAllowed}" Value="false">
<Setter Property="IsEnabled" Value="false"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>

Related

(WPF, C#, ListView) How can make event when user selected item

It's have to double click(click for select item=> click same item for event, it is almost like double click).
I want to make just one click event(when item select).
**Listview.xaml**
<ListView x:Name="listV1" HorizontalAlignment="Left" Height="263"
Margin="44,159,0,0" VerticalAlignment="Top" Width="283" BorderThickness="0" FontFamily="NanumSquareOTF" FontSize="18">
<ListView.View >
<GridView ColumnHeaderContainerStyle="{StaticResource myHeaderStyle}">
<GridViewColumn Header="Title" DisplayMemberBinding="{Binding TITLE}" Width="250" />
</GridView>
</ListView.View>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDown1" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
**Listview.xaml.cs**
private void ListViewItem_PreviewMouseLeftButtonDown1(object sender, MouseButtonEventArgs e)
{
var item = sender as ListViewItem;
if (item != null && item.IsSelected)
{
uid_tmp = "";
DbData selectedItem = (DbData)listV1.SelectedItems[0];
db_tmp = selectedItem.Db;
Get_UIDDataAsync();
}
}
I solved use SelectionChanged
**list.xalm**
<ListView x:Name="listV1" HorizontalAlignment="Left"
Height="263" Margin="44,159,0,0" VerticalAlignment="Top"
Width="283" BorderThickness="0" FontFamily="NanumSquareOTF"
FontSize="18"
SelectionMode="Single"
SelectionChanged="listV1_SelectionChanged"
>
<ListView.View >
<GridView ColumnHeaderContainerStyle="{StaticResource myHeaderStyle}">
<GridViewColumn Header="Title" DisplayMemberBinding="{Binding TITLE}" Width="250" />
</GridView>
</ListView.View>
</ListView>
**list.xaml.cs**
private void listV1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = listV1.SelectedItem;
if (item != null)
{
uid_tmp = "";
DbData selectedItem = (DbData)item;
db_tmp = selectedItem.Db;
Get_UIDDataAsync();
}
}

ListView grouping - missing text (WPF, c#, XAML)

I'm trying display ListView grouping. I did as below:
ListView.xaml.cs
ListViewModel lvm = new ListViewModel();
lvUsers3.ItemsSource = lvm.getData();
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvUsers3.ItemsSource);
PropertyGroupDescription groupDescription = new PropertyGroupDescription("Type");
view.GroupDescriptions.Add(groupDescription);
ListView.xaml
<ListView Name="lvUsers3" Margin="0,197,94,0" ItemsSource="{Binding tvq}">
<ListView.View>
<GridView>
<GridViewColumn Header="FirstName" Width="100" DisplayMemberBinding="{Binding FirstName}" />
<GridViewColumn Header="LastName" Width="100" DisplayMemberBinding="{Binding LastName}" />
</GridView>
</ListView.View>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock FontWeight="Bold" FontSize="14" FontStyle="Normal" Text="{Binding Type}" />
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
ListViewModel.cs
namespace LayoutMVVM.ViewModels
{
public class ListViewModel
{
public List<test_view> getData()
{
testViewClassDataContext tv = new testViewClassDataContext();
List<test_view> tvq = (from tt in tv.test_views
select tt).ToList();
return tvq;
}
}
}
On result I see that is grouped but the "type" value is missing (like was hidden?)
When you have applied the grouping you have created a group object that contains your sub items and this is what you are binding your group header to
the group object doesn't contain a Type property to bind to
try "{Binding Name}" instead of "{Binding Type}"

WPF ListView group and sort

I'm trying to group items on my list and sort them from oldest to newest. Grouping works perfect, it just doesn't want to inverse this list.
WPF Code:
<ListView x:Name="lst_orders" Margin="5" GridViewColumnHeader.Click="results_Click">
<ListView.Resources>
<Style TargetType="ListViewItem">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="true">
<Setter Property="IsSelected" Value="true" />
</Trigger>
</Style.Triggers>
</Style>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button x:Name="goto_parts" Content="Show parts" Width="AUTO" Padding="2" Margin="2" Background="#FF179917" Click="goto_parts_Click"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Order ID" Width="100" DisplayMemberBinding="{Binding fullOrderId}" />
<GridViewColumn Header="Job Status" Width="100" DisplayMemberBinding="{Binding status}" />
<GridViewColumn Header="Order Type" Width="150" DisplayMemberBinding="{Binding orderType}" />
<GridViewColumn Header="Customer Notes" Width="250" DisplayMemberBinding="{Binding notes}" />
<GridViewColumn Header="Admin Notes" Width="250" DisplayMemberBinding="{Binding adminNotes}" />
<GridViewColumn Header="Production Notes" Width="100" DisplayMemberBinding="{Binding production_notes}" />
</GridView>
</ListView.View>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Background="Gray">
<TextBlock Text="Days: " FontSize="18" FontWeight="Bold"/>
<TextBlock Text="{Binding Name}" FontSize="18" FontWeight="Bold"/>
<TextBlock Text=" Systems: " FontSize="18" FontWeight="Bold"/>
<TextBlock Text="{Binding ItemCount}" FontSize="18" FontWeight="Bold"/>
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
C# Code:
ICollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lst_orders.ItemsSource);
PropertyGroupDescription groupDescription = new PropertyGroupDescription("daysOld");
view.GroupDescriptions.Add(groupDescription);
view.SortDescriptions.Add(new SortDescription("daysOld", ListSortDirection.Descending));
lst_orders.ItemsSource = view;
daysOld is a property that calculates difference between today's date and order date, works perfect, but I want to see it from the highest number to the lowest. Picture shows what I get, and I want to reverse it.
Your code is correct.I've made a test. It is possible to see in view:
C#:
List<SomeClass> list = new List<SomeClass>();
Random rnd = new Random();
for (int i = 0; i < 10; i++)
{
list.Add(new SomeClass() { DaysOld = DateTime.Now.Add(new TimeSpan(0, rnd.Next(25), 0))});
}
ICollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(list);
PropertyGroupDescription groupDescription = new PropertyGroupDescription("daysOld");
view.GroupDescriptions.Add(groupDescription);
view.SortDescriptions.Add(new SortDescription("DaysOld", ListSortDirection.Descending));
listBox.ItemsSource = view;
listBox.DisplayMemberPath = "DaysOld";
XAML:
<ListBox Name="listBox"/>
Model:
public class SomeClass
{
public DateTime DaysOld { get; set; }
}
Image:

listview with checkbox get selected row index

here is my problem , i have a listview with checkbox , and i want to get the index of the row which is selected
i want to get the index of the row to disable this row after validation
i try different method
CheckBox cbx = sender as CheckBox.tag;
if (cbx != null) {
var index = cbx.Tag.ToString();
}
(((ContentPresenter)((CheckBox)sender).TemplatedParent)).IsEnabled = false; with this i disable just the checkbox
CheckBox cbx = sender as CheckBox.tag;
int index = (int)(sender as CheckBox).Tag;
Use Below Code For One Row:
int index = YourListView.SelectedIndex;
Try below sample code.
Add a property as below
public int Selected
{
get { return _selected; }
set
{
_selected = value;
OnPropertyChanged(new PropertyChangedEventArgs("Selected"));
}
}
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
Inherit INotifyPropertyChanged into your cs file to get OnPropertyChanged
In the view bind the above property in listview
<Grid>
<ListView Margin="10" Name="lvUsers" SelectedIndex="{Binding Selected}">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Tag="{Binding ID}" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Path=IsSelected}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
<GridViewColumn Header="Mail" Width="150" DisplayMemberBinding="{Binding Mail}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
Try checking if the checkbox is disabled or not with this property. I hope this will works for you.

How to get row data of each column if button is clicked inside listview

I am new to WPF. I have just bind one listview. Under my listview there is a button in each row.
When user clicks on that button, the data from that particular row should be fetched.
Below is my code
<Window x:Class="Searching.ImportedKeywords"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ImportedKeywords" Height="330.769" Width="1079.12">
<Grid Margin="-1094,0,2,24" RenderTransformOrigin="0.621,0.497">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="0*"/>
</Grid.ColumnDefinitions>
<ListView Name="lvShowSearching" Height="247" VerticalAlignment="Top" RenderTransformOrigin="2.25,3.86" Margin="1100,10,0,-13">
<ListView.View>
<GridView>
<GridViewColumn Header="First Name" Width="70" DisplayMemberBinding="{Binding FirstName}" />
<GridViewColumn Header="Last Name" Width="70" DisplayMemberBinding="{Binding LastName}" />
<GridViewColumn Header="With All Of The Words" Width="100" DisplayMemberBinding="{Binding WithAllOfTheWords}" />
<GridViewColumn Header="With The Exact Phrase" Width="100" DisplayMemberBinding="{Binding WithTheExactPhrase}" />
<GridViewColumn Header="With At Least One Of These Words" Width="100" DisplayMemberBinding="{Binding WithAtLeastOneOfTheseWords}" />
<GridViewColumn Header="Without The Word" Width="100" DisplayMemberBinding="{Binding WithoutTheWord}" />
<GridViewColumn Header="Country" Width="70" DisplayMemberBinding="{Binding CountryName}"/>
<GridViewColumn Header="State" Width="70" DisplayMemberBinding="{Binding StateName}" />
<GridViewColumn Header="City" Width="70" DisplayMemberBinding="{Binding CityName}" />
<GridViewColumn Header="Publication" Width="70" DisplayMemberBinding="{Binding PublicationName}" />
<GridViewColumn Header="Total Records" Width="70" DisplayMemberBinding="{Binding TotalRecords}" />
<GridViewColumn Header="Total Records Imported" Width="70" DisplayMemberBinding="{Binding TotalRecordsImported}" />
<GridViewColumn Header="Status" Width="70" DisplayMemberBinding="{Binding Status}" />
<GridViewColumn Header="Import" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button CommandParameter="{Binding}" Click="Button_Click_1">Import</Button>
<!--<TextBlock Text="{Binding Mail}" TextDecorations="Underline" Foreground="Blue" Cursor="Hand" />-->
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Address">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Margin="6,2,6,2">
<Button Content="Address"
Command="{Binding
Path=DataContext.RunCommand,
RelativeSource=
{RelativeSource FindAncestor,
AncestorType={x:Type ItemsControl}}}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
private void Button_Click_1(object sender, RoutedEventArgs e)
{
IEnumerable items = this.lvShowSearching.Items;
foreach (ClippingSearchKeyword data in items)
{
if (data != null)
{
}
}
}
private void OnRunCommand(object obj)
{
if (obj != null)
{
}
}
You can find your parent GridViewRow using below method
public static T FindVisualParent<T>(UIElement element) where T : UIElement
{
UIElement parent = element;
while (parent != null)
{
T correctlyTyped = parent as T;
if (correctlyTyped != null)
{
return correctlyTyped;
}
parent = VisualTreeHelper.GetParent(parent) as UIElement;
}
return null;
}
And change ButtonClick event as below,You will get GridViewRow
private void Button_Click_1(object sender, RoutedEventArgs e)
{
GridViewRow grvRow= FindVisualParent<GridViewRow>(sender as UIElement);
}

Categories

Resources