There is a scroll viewer that contains a list view and there is list view on the sroll viewr. and when scroll on the list view it doesn't scroll but when you scroll out the list view(on the area of scroll viewer that is not behind list view).How can I fix this?
My code:
<ScrollViewer Background="#111" HorizontalScrollBarVisibility="Disabled" FlowDirection="LeftToRight" Grid.Row="1" >
<Grid Grid.Row="1">
<StackPanel Background="#111" HorizontalAlignment="Center">
<ListView BorderThickness="2" HorizontalAlignment="Right" ItemsSource="{Binding MyList}">
//My codes
</ListView>
</StackPanel>
</Grid>
</ScrollViewer>
A ScrollViewer enables content to be displayed in a smaller area than its actual size. For scrolling all elements, you could try to refer to the following code.
<Window x:Class="ScrollDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ScrollDemo"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<ScrollViewer>
<Grid Height="500">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<TextBlock Text="scroll"/>
<ScrollViewer Background="#111" HorizontalScrollBarVisibility="Disabled" FlowDirection="LeftToRight" Grid.Row="1" >
<ListView x:Name="lv" BorderThickness="2" HorizontalAlignment="Right" ItemsSource="{Binding }">
<ListView.View>
<GridView>
<GridViewColumn Width="100" Header="Name" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Width="100" Header="Age" DisplayMemberBinding="{Binding Age}"/>
</GridView>
</ListView.View>
</ListView>
</ScrollViewer>
</Grid>
</ScrollViewer>
</Window>
MainWindow.xaml.cs:
using System.Collections.Generic;
using System.Windows;
namespace ScrollDemo
{
public partial class MainWindow : Window
{
List<Item> MyList;
public MainWindow()
{
InitializeComponent();
MyList = new List<Item>();
for (int i = 0; i < 10; i++)
{
MyList.Add(new Item(){ Name= $"list{ i }",Age=i });
}
lv.ItemsSource=MyList;
}
}
public class Item
{
public string Name { get;set;}
public int Age { get;set;}
}
}
The result
Good day,
I would like to ask question on how to properly bind the RelayCommand UpdateUserCommand() in toggle button. Please see my code below
<UserControl x:Class="istock.View.UserMasterList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:local="clr-namespace:istock.View"
mc:Ignorable="d"
MinHeight="450" Width="Auto" MinWidth="1024" FontFamily="Segoe UI LIght">
<Grid>
<Border Padding="30 15">
<StackPanel>
<Grid>
<TextBlock Text="User Management" FontSize="20" HorizontalAlignment="Left" Width="Auto" />
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Width="Auto">
<Button x:Name="btnNew" Foreground="#FFFF">
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="PlusBoxOutline" Margin="0 5" />
<TextBlock Text="New User" Margin="5 3 5 0" />
</StackPanel>
</Button>
</StackPanel>
</Grid>
<ListView ItemsSource="{Binding Path = UserMasterListProp}">
<ListView.View>
<GridView>
<GridViewColumn Header="ID" Width="auto" DisplayMemberBinding="{Binding Path = Id}"></GridViewColumn>
<GridViewColumn Header="Username" Width="120" DisplayMemberBinding="{Binding Path = Username}"></GridViewColumn>
<GridViewColumn Header="Firstname" Width="120" DisplayMemberBinding="{Binding Path = Firstname}"></GridViewColumn>
<GridViewColumn Header="Middlename" Width="120" DisplayMemberBinding="{Binding Path = Middlename}"></GridViewColumn>
<GridViewColumn Header="Lastname" Width="120" DisplayMemberBinding="{Binding Path = Lastname}"></GridViewColumn>
<GridViewColumn Header="Role" Width="100" DisplayMemberBinding="{Binding Path = Role}"></GridViewColumn>
<GridViewColumn Header="Activated">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ToggleButton x:Name="tbtnActivated" Style="{DynamicResource MaterialDesignSwitchToggleButton}" IsChecked="{Binding Path = Activated}" Command="{Binding Path = UserViewModel.UpdateUserCommand}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Locked">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ToggleButton x:Name="tbtnLocked" Style="{DynamicResource MaterialDesignSwitchToggleButton}" IsChecked="{Binding Path = Locked}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</Border>
</Grid>
Here is my Command Code which was initialized and CanExecute() method is always true.
public class RelayCommand : ICommand
{
private Action ExecuteCmd;
public RelayCommand(Action paramAction)
{
this.ExecuteCmd = paramAction;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
this.ExecuteCmd();
}
}
And Here's my code in ViewModel which the UpdateUser() method will be fired
by my UpdateUserCommand()
#region Update Command and Method
private RelayCommand updateUserCommand;
public RelayCommand UpdateUserCommand
{
get { return updateUserCommand; }
}
public void UpdateUser()
{
try
{
var isExist = this.userService.ApplyService_FindByIdOrUsername(UserModel.Id, UserModel.Username);
if(isExist == null)
{
var isUpdated = this.userService.ApplyService_Update(UserModel);
if (isUpdated == true)
{
this.Message = "Update sucesss.";
this.Populate_UserMasterList();
}
}
else
{
this.Message = "Update failed. ID or Username already exist.";
return;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
return;
}
}
#endregion
Here is the implementation of the Data Context in Code Behind of the User Control
public partial class UserMasterList : UserControl
{
public UserMasterList()
{
InitializeComponent();
UserViewModel userViewModel = new UserViewModel();
this.DataContext = userViewModel;
}
}
Please help. If ever my code is confusing, I am open for question. Thanks
You could bind to a property of the parent UserControl using a {RelativeSource}:
<ToggleButton x:Name="tbtnActivated"
Style="{DynamicResource MaterialDesignSwitchToggleButton}"
IsChecked="{Binding Activated}"
Command="{Binding DataContext.UpdateUserCommand,
RelativeSource={RelativeSource AncestorType=UserControl}}" />
I am attempting to make a simple VS 2017 Extension that is taking a object and displaying it. I have the data coming back and displaying the json in a text box, so I know the data is coming back correctly. But for some reason the gv is just showing the word "id" twice, as their are two records in the dataset. I have tried so many things I'm loosing track. Plus the documentation seems to be all over the place.
I believe there could be at least 2 issues here...
1) XAML the "Bindings"
2) Binding or adding the data to the LV?
Any help with this would be greatly appreciated!
XAML
<UserControl x:Class="DoWork.AllWorkVSControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Background="{DynamicResource VsBrush.Window}"
Foreground="{DynamicResource VsBrush.WindowText}"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Name="MyToolWindow">
<Grid>
<StackPanel Orientation="Vertical">
<TextBlock Margin="10" HorizontalAlignment="Center">AllWorkVS</TextBlock>
<Button Content="Click me!" Click="button1_Click" Width="120" Height="80" Name="button1"/>
<TextBox Height="200" TextWrapping="Wrap" Name="txtJson"/>
<ListView x:Name="LvIssues">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Width="Auto" Header="Id" DisplayMemberBinding="{Binding Source='Id'}"></GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</Grid>
</UserControl>
C#
public class People
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public partial class AllWorkVSControl : UserControl
{
public AllWorkVSControl()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
var t = Issues.GetPeopleById("2");
PopulateListView();
MessageBox.Show(t);
}
private void PopulateListView()
{
var l = GetPeople();
txtJson.Text = JsonConvert.SerializeObject(l);
foreach (var p in l)
{
LvIssues.Items.Add(p);
}
}
}
you need to set the ListView.ItemsSource.
private void PopulateListView()
{
var l = GetPeople();
txtJson.Text = JsonConvert.SerializeObject(l);
LvIssues.ItemsSource= l;
}
<ListView x:Name="LvIssues">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Width="Auto" Header="Id" DisplayMemberBinding="{Binding Id}"></GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
I'm struggling to bind 2 listviews selected items to my viewmodel.
The idea is to have a view with 2 listviews and 2 buttons in between to move the (multiple) selected items from 1 listview to another (2 way).
The view:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ListView x:Name="DuamResources" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ListView.View>
<GridView>
<GridViewColumn Header="Id" DisplayMemberBinding="{Binding Id}"/>
<GridViewColumn Header="Description" DisplayMemberBinding="{Binding Name}"/>
</GridView>
</ListView.View>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock FontWeight="Bold" FontSize="14" Text="{Binding Name}"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
<StackPanel Grid.Column="1" Orientation="Vertical" cal:Action.TargetWithoutContext="{Binding SelectedDuamResources}" VerticalAlignment="Center">
<Button Content="<" Width="40" Margin="5" cal:Message.Attach="AddSelectedResources($datacontext)"/>
<Button Content=">" Width="40" Margin="5"/>
</StackPanel>
<ListView x:Name="AvailableDuamResources" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ListView.View>
<GridView>
<GridViewColumn Header="Id" DisplayMemberBinding="{Binding Id}"/>
<GridViewColumn Header="Description" DisplayMemberBinding="{Binding Name}"/>
</GridView>
</ListView.View>
<ListView.GroupStyle>
<GroupStyle HidesIfEmpty="True">
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock FontWeight="Bold" FontSize="14" Text="{Binding Type}"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
</Grid>
The associated viewmodel properties:
private ObservableCollection<DuamResourceInfo> availableDuamResources;
public ObservableCollection<DuamResourceInfo> AvailableDuamResources
{
get { return availableDuamResources; }
set { availableDuamResources = value; NotifyOfPropertyChange(() => AvailableDuamResources); }
}
private ObservableCollection<DuamResourceInfo> selectedDuamResources;
public ObservableCollection<DuamResourceInfo> SelectedDuamResources
{
get { return selectedDuamResources; }
set { selectedDuamResources = value; NotifyOfPropertyChange(() => SelectedDuamResources); }
}
private ObservableCollection<DuamResourceInfo> duamResources;
public ObservableCollection<DuamResourceInfo> DuamResources
{
get { return duamResources; }
set { duamResources = value; NotifyOfPropertyChange(() => DuamResources); }
}
The code I want to achieve:
public void AddSelectedResources(IEnumerable<DuamResourceInfo> resources)
{
}
public void RemoveSelectedResources(IEnumerable<DuamResourceInfo> resources)
{
}
I'm using Caliburn Micro for MVVM. I know there are some conventions for binding but I'm not sure they apply to this scenario.
Any help is greatly appreciated!
Have you tried using "x:Bind" instead of "Binding"? For some reason in the past one or the other has worked for me, but not both, depending on the situation.
Goal:
Change the listview's background color to light colored grey if the current time pass the appointment time. For instance, the time is 10.15 and you have background color for the 09.30 - 10.30 and 10.00 - 11.00.
Problem:
I do not know what syntax I need to use in order achieve the goal. I also reviews some advice from different website and their advice is not enough.
Information:
- I'am using Nhibernate to retrive the data.
- This is a usercontrol.
private void DisplayActivityBasedonSelectedDate()
{
DateTime aa = (DateTime) cdate_left.SelectedDate;
lvw_aktivitet.DataContext = _myNhibernateDataProvider.RetrieveAllActivitetBasedOnDate(aa);
DisplayDateInTheLabel();
}
public IList<Aktivitet_data> RetrieveAllActivitetBasedOnDate(DateTime pDatum)
{
return _session.GetNamedQuery("sp_retrieveAllActivitetBasedOnDate").SetDateTime("Datum", pDatum)
.SetResultTransformer(Transformers.AliasToBean(typeof(Aktivitet_data))).List<Aktivitet_data>();
}
public class Aktivitet_data
{
public string PK_Aktivitet_schema { get; set; }
public string Datum { get; set; }
public string Tid { get; set; }
public string Aktivitet { get; set; }
public string Total_platser { get; set; }
public string Bokade { get; set; }
public string Drop_in { get; set; }
}
XAML:
<UserControl x:Class="usercontrol_bokning.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid Background="White" Margin="-127,0,-777,-316">
<ListView x:Name="lvw_aktivitet" HorizontalAlignment="Left" ItemsSource="{Binding}" SelectionMode="Single" Height="337" Margin="217,147,0,0" VerticalAlignment="Top" Width="365" SelectionChanged="lvw_aktivitet_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn Header="Tid" Width="auto" DisplayMemberBinding="{Binding Path=Tid}" TextBlock.TextAlignment="Left" />
<GridViewColumn Header="Aktivitet" Width="auto" DisplayMemberBinding="{Binding Path=Aktivitet}" TextBlock.TextAlignment="Left" />
<GridViewColumn Header="Total platser" Width="auto" DisplayMemberBinding="{Binding Path=Total_platser}" TextBlock.TextAlignment="Center" />
<GridViewColumn Header="Bokade" Width="auto" DisplayMemberBinding="{Binding Path=Bokade}" TextBlock.TextAlignment="Center" />
<GridViewColumn Header="Drop in" Width="auto" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock TextAlignment="Right" Block.TextAlignment="Right" Text="{Binding Path=Drop_in}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Calendar x:Name="cdate_left" HorizontalAlignment="Left" Margin="10,147,0,0" VerticalAlignment="Top" SelectedDatesChanged="cdate_left_SelectedDatesChanged"/>
<Button x:Name="btn_idag" Content="Idag" HorizontalAlignment="Left" Margin="217,122,0,0" VerticalAlignment="Top" Width="75" Click="btn_idag_Click"/>
<Button x:Name="arrow__left" Click="Arrow_left_Click" Background="Transparent">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Image x:Name="bild_arrow_left" HorizontalAlignment="Left" Height="22" Margin="302,121,0,0" VerticalAlignment="Top" Width="22" Source="C:\Users\Administrator\Downloads\left.jpg" />
</ControlTemplate>
</Button.Template>
</Button>
<Button x:Name="arrow__right" Click="Arrow_right_Click" Background="Transparent" >
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Image x:Name="bild_arrow_right" HorizontalAlignment="Left" Height="22" Margin="326,121,0,0" VerticalAlignment="Top" Width="22" Source="C:\Users\Administrator\Downloads\right.jpg"/>
</ControlTemplate>
</Button.Template>
</Button>
<Label x:Name="lbl_DisplayDateBySelection" Content="" HorizontalAlignment="Left" Margin="396,119,0,0" VerticalAlignment="Top"/>
<TabControl HorizontalAlignment="Left" Height="420" Margin="603,75,0,0" VerticalAlignment="Top" Width="589">
<TabItem Header="Bokade">
<Grid Background="#FFE5E5E5">
<ListView x:Name="lvw_bokade" HorizontalAlignment="Left" Height="335" Margin="10,46,0,0" VerticalAlignment="Top" Width="561">
<ListView.View>
<GridView>
<GridViewColumn/>
</GridView>
</ListView.View>
</ListView>
<Button Content="Ny plats" HorizontalAlignment="Left" Margin="10,21,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</TabItem>
<TabItem Header="Avbokade">
<Grid Background="#FFE5E5E5" Margin="0,1,0,-1"/>
</TabItem>
<TabItem Header="Avregistrerade" HorizontalAlignment="Left" Height="20" VerticalAlignment="Top" Width="55">
<Grid Background="#FFE5E5E5"/>
</TabItem>
</TabControl>
<Label x:Name="lbl_instruktor" Content="Instruktör: " HorizontalAlignment="Left" Margin="1009,122,0,0" VerticalAlignment="Top"/>
</Grid>
</UserControl>
If you want it to update the items automatically you'll have to use some sort of a timer.
I used DispatcherTimer set globally in the UserControl
public DispatcherTimer dispatcher;
On Loaded / Ctor
dispatcher = new DispatcherTimer();
dispatcher.Tick += CheckTime;
dispatcher.Interval = new TimeSpan(0, 0, 5); //Change the interval to whatever suits you best
dispatcher.Start();
and the method to check your data:
private void CheckTime(object sender, EventArgs e)
{
ListBoxItem lbitem;
foreach (var item in lvw_aktivitet.Items)
{
if ((item as Aktivitet_data).Tid > DateTime.Now)
{
lbitem = (ListBoxItem)lvw_aktivitet.ItemContainerGenerator.ContainerFromItem(item);
lbitem.Background = new SolidColorBrush(Colors.Gray);
}
}
}