使用MVVM(没有代码隐藏),我想在选择时隐藏我的DataGrid列,我有以下代码:
<DataGrid ItemsSource="{Binding SSID}" Grid.Row="1" Margin="10,10,0,0" Height="200" Width="500" Grid.ColumnSpan="2" Name="dg" HorizontalAlignment="Left" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Network ID" Binding="{Binding _networkID}"></DataGridTextColumn>
<DataGridTextColumn Header="SSID" Binding="{Binding _ssid}"></DataGridTextColumn>
<DataGridTextColumn Header="VLAN" Binding="{Binding _vlan}"></DataGridTextColumn>
<DataGridTextColumn Visibility="{Binding _authenticationMode}" Binding="{Binding _authenticationMode}"></DataGridTextColumn>
<DataGridTextColumn Visibility="{Binding _authentication}" Binding="{Binding _authentication}"></DataGridTextColumn>
<DataGridTextColumn Visibility="{Binding _staticWEPKeyType}" Binding="{Binding _staticWEPKeyType}"></DataGridTextColumn>
<DataGridTextColumn Visibility="{Binding _staticWEPKeyLength}" Binding="{Binding _staticWEPKeyLength}"></DataGridTextColumn>
<DataGridTextColumn Visibility="{Binding _staticWEPKey1}" Binding="{Binding _staticWEPKey1}"></DataGridTextColumn>
<DataGridTextColumn Visibility="{Binding _staticWEPKey2}" Binding="{Binding _staticWEPKey2}"></DataGridTextColumn>
<DataGridTextColumn Visibility="{Binding _staticWEPKey3}" Binding="{Binding _staticWEPKey3}"></DataGridTextColumn>
<DataGridTextColumn Visibility="{Binding _staticWEPKey4}" Binding="{Binding _staticWEPKey4}"></DataGridTextColumn>
<DataGridTextColumn Visibility="{Binding _wpaPersonalKeyAC}" Binding="{Binding _wpaPersonalKeyAC}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
C#代码是:
var ssid = new SSIDPropertyClass();
ssid._networkID = SSID.Count + 1;
ssid._ssid = EnteredSSIDAC;
ssid._vlan = VlanSSID;
if (ACSelectedSecurityType=="Static WEP")
{
ssid._authenticationMode = ACSelectedSecurityType;
ssid._authentication = ACStaticWEPSelectedAuthentication;
ssid._staticWEPKeyType = ACStaticWEPSelectedKeyType;
ssid._staticWEPKeyLength = ACStaticWEPSelectedKeyLength;
ssid._staticWEPKey1 = StaticWEPKey1;
ssid._staticWEPKey2 = StaticWEPKey2;
ssid._staticWEPKey3 = StaticWEPKey3;
ssid._staticWEPKey4 = StaticWEPKey4;
SSID.Add(ssid);
}
else if(ACSelectedSecurityType=="WPA/WPA2 Personal")
{
ssid._authenticationMode = ACSelectedSecurityType;
ssid._wpaPersonalKeyAC = WpaACKey;
SSID.Add(ssid);
}
我希望当执行块时,它只会在datagrid中添加块列,而在块中添加其他其他块的情况。实际上,它总是显示我不需要的额外列,例如当{{1执行我想只显示我在SSID集合中添加的两个,但是它也显示了阻塞列。所以简而言之我想让额外列的可见性为false。任何人都可以解决我的问题,因为我需要明天提交。任何帮助都会非常明显吗?
答案 0 :(得分:28)
如果要隐藏列,则需要像这样指定属性Visibility
,
YourDataGrid.Columns[IndexOftheColumn].Visibility = Visibility.Collapsed;
如果要隐藏第一列,请说“网络ID”
dg.Columns[0].Visibility = Visibility.Collapsed;
答案 1 :(得分:2)
对于那些希望在XAML中隐藏它的人
它看起来像这样:
<DataGridTextColumn Visibility="Collapsed" Header="Merchant Reference" Binding="{Binding MerchantReference}" Width="200" />
答案 2 :(得分:2)
您不必隐藏列,只需将其MaxWidth属性设置为零,它就不会出现。
DataGrid.Columns[IndexOfColumn].MaxWidth = 0;
答案 3 :(得分:0)
在MVVM中,这通常是通过ValueConverter处理的
public sealed class BoolToVisibilityConverter : IValueConverter
{
public Visibility TrueValue { get; set; }
public Visibility FalseValue { get; set; }
public BoolToVisibilityConverter()
{
// Set defaults
this.TrueValue = Visibility.Visible;
this.FalseValue = Visibility.Collapsed;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool flag = false;
if (value is bool)
{
flag = (bool)value;
}
else if (value is bool?)
{
bool? nullable = (bool?)value;
flag = nullable.HasValue ? nullable.Value : false;
}
return flag ? this.TrueValue : this.FalseValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (object.Equals(value, this.TrueValue))
return true;
if (object.Equals(value, this.FalseValue))
return false;
return null;
}
}
在资源部分添加
<ResourceDictionary>
<namspace:BoolToVisibilityConverter x:Key="BoolToCollapsed" TrueValue="Visible" FalseValue="Collapsed" />
</ResourceDictionary>
在您的数据网格上
<DataGrid ItemsSource="{Binding Items}" >
<DataGrid.Columns>
<DataGridTextColumn Visibility="{Binding ShowThisColumnFlag, Converter={StaticResource BoolToCollapsed}}" Binding="{Binding PropertyOne}" />
<DataGridTextColumn Visibility="{Binding ShowAnotherColumnFlag, Converter={StaticResource BoolToCollapsed}}" Binding="{Binding PropertyTwo}"/>
</DataGrid.Columns>
</DataGrid>
您应该根据需要设置模式。