c#


MVVM Setting GridViewColumn width dynamically


In my mvvm application I have a listview.
I like to show/hide some columns of the
listview, depending on the state of the checkbox "Show all columns" (Here: Col1 should be showed/hidden).
That's my very simplified code. What is wrong?!
Obviously this does't work!
<Window x:Class="WpfApplication1.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:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="185">
<Window.Resources>
<local:ConverterHideListViewColumn x:Key="ConverterHideListViewColumn" />
</Window.Resources>
<Grid>
<ListView Height="100" Width="100">
<ListView.View>
<GridView>
<GridViewColumn Header="Col0" Width="40"/>
<GridViewColumn Header="Col1" Width="{Binding ShowAllColumns, Converter={StaticResource ConverterHideListViewColumn}}"/>
</GridView>
</ListView.View>
</ListView>
<CheckBox
Content="Show all columns"
IsChecked="{Binding ShowAllColumns, Mode=TwoWay}"
Margin="40,140,0,0">
</CheckBox>
</Grid>
</Window>
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
VM _vm;
public MainWindow ()
{
InitializeComponent ();
_vm = new VM ();
this.DataContext = _vm;
}
}
/// <summary>
/// Dummy Viewmodel
/// </summary>
public class VM : INotifyPropertyChanged
{
private bool _bShowAllColumns;
public event PropertyChangedEventHandler PropertyChanged;
public VM ()
{
ShowAllColumns = true;
}
public bool ShowAllColumns
{
get { return _bShowAllColumns; }
set { _bShowAllColumns = value; }
}
private void OnPropertyChanged (string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler (this, new PropertyChangedEventArgs (propertyName));
}
}
/// <summary>
/// Converter for setting the ListView-Column width, depending on value VM.ShowAllColumns
/// </summary>
class ConverterHideListViewColumn : IValueConverter
{
public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool bShowAllColumns = false;
double dWidth = 0;
if (value is bool)
{
bShowAllColumns = (bool) value;
dWidth = bShowAllColumns? 40 : 0;
}
return dWidth;
}
public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException ();
}
}
}
A GridViewColumn is not added to the visual tree and doesn't inherit any DataContext so you cannot bind its Width property to a source property of a view model without using a BindingProxy class as suggested here: https://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/
This is all you need...
public bool ShowAllColumns
{
get { return _bShowAllColumns; }
set
{
if (_bShowAllColumns != value)
{
_bShowAllColumns = value;
OnPropertyChanged("ShowAllColumns");
}
}
}

Related Links

How to ensure the SQL is able to read all XML tag data
ContactPicker is not working in Windows Phone 8.1 Silverlight
Is WP Silverlight 8.1 application installed on SD card or on internal phone memory?
View with multiple tables
Move switch statement to text file
Messages not being received from Azure Service Bus Queue
Performing a time calculation inside a Linq query
Error With Stored Procedure Code
Button Visibility Inside DataGrid WPF
Confirm box in asp.net web form application
TargetException on add to list
MVVM light - push notifications
How is “as” operator translated when the right-side operand is generic?
Reflection - how to compare between list of types
C# XML document remove elements that fail schema - preferably split the file into valid and invalid
HtmlAgilityPack scraping “href”

Categories

HOME
joomla
logging
botframework
jenkins-pipeline
popover
highmaps
itext7
foreach
pdf-generation
revit-api
distinct
infopath
ibatis
xforms
fibonacci
matlab-gui
ng-tags-input
onclicklistener
oracle10g
rancher
orgchart
setup-project
git-submodules
foxpro
akamai
ng2-bootstrap
matlab-compiler
dropdownbox
intersection
device-driver
jslider
aspose.pdf
ssdp
cakephp-3.2
bootstrap-carousel
api-key
qtwebkit
e4
openpgp.js
ajv
sp
rmq
ios-keyboard-extension
apache-commons-cli
recursive-query
sqlproj
dbi
settext
konakart
hoare-logic
instabug
jquery-draggable
cascading
web-performance
random-sample
redquerybuilder
dnvm
syncano
borland-c++
targetprocess
teamwork
skview
firebase-tools
uiblureffect
frisby.js
grunt-contrib-connect
diffmerge
formbuilder
javacompiler
ccmenuitem
ant-contrib
yandex-api
traceur
rssi
sqldatasource
codio
vbaccelerator
code-complete
listpicker
source-depot
stringstream
leap-year
uikit-dynamics
exi
gwt2
spawn
pymel
antixsslibrary
idoc
hypertable
xcode4.6.3
fileoutputstream
code-organization
jqzoom
clgeocoder
file-processing
auto-compile
linq-to-mysql
authkit
file-icons

Resources

Encrypt Message