Tengo un StackPanel
que quiero hacer visible solo cuando SomeTabControl.SelectedItem != null
. ¿Cómo hago esto en el enlace de WPF?wpf databind IsVisible to TabControl.SelectedItem! = Null
Respuesta
usted puede hacerlo sin un convertidor mediante el uso de un estilo y el gatillo:
<StackPanel>
<StackPanel.Style>
<Style TargetType="{x:Type StackPanel}">
<Setter Property="Visibility" Value="Visible" />
<Style.Triggers>
<DataTrigger
Binding="{Binding SelectedItem,ElementName=tabControl1}"
Value="{x:Null}">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
<Style.Triggers>
</Style>
</StackPanel.Style>
</StackPanel>
Este ejemplo muestra el StackPanel por defecto, pero luego lo esconde cuando el SelectedItem en TabControl1 es nulo.
Cree un convertidor que convierta un valor que admite valores de nulo en un valor System.Windows.Visibility y utilícelo en su enlace.
Por ejemplo:
<StackPanel x:Name="myPanel" Visibility="{Binding Path=SelectedItem, Mode=OneWay, ElementName=SomeTabControl, Converter={StaticResource visibilityConverter}}" />
Código de la clase convertidor:
public class VisibilityConverter : IValueConverter
{
#region [ IValueConverter ]
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if(value == null)
return System.Windows.Visibility.Collapsed;
return System.Windows.Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
#endregion
}
P. S. Esto supone que en el XAML de su control hay un recurso estático llamado visibilityConverter.
Puede usar el built in BoolToVisiblityConverter en lugar de escribir el suyo. http://msdn.microsoft.com/en-us/library/system.windows.controls.booleantovisibilityconverter.aspx – Andy
¿Cómo puede BoolToVisibilityConverter convertir! = null a Visible? Si fue verdadero o falso, lo entiendo. Pero! = Nulo? – azazeal
- 1. WPF Databind Before Saving
- 2. Silverlight programmatic databind to Page Title Propiedad no actualizando
- 3. Postgres COPY TO NULL enteros
- 4. databind la propiedad Source del WebBrowser en WPF
- 5. wpf datagrid databind con objetos anidados (como el detalle maestro)
- 6. LINQ to Entities and null strings
- 7. Django Model Field Default to Null
- 8. WPF listbox copy to clipboard
- 9. ¿Databind y valida un TableViewer?
- 10. WPF Custom Control: TemplateBinding to Image
- 11. WPF - FindName devuelve NULL cuando no debería
- 12. Linq To Xml Null Comprobación de los atributos
- 13. C# Events and Lambdas, alternative to null check?
- 14. DataBind y devolución de datos
- 15. Enlace Image.Source to String en WPF?
- 16. ApplicationBar siempre es NULL
- 17. Diferencia entre isElementPresent e isVisible en Selenium RC
- 18. Marcador de fecha Bootstrap con databind knockout.js
- 19. - WPF
- 20. Agregar nueva fila a GridView sin DataBind
- 21. Archivo de recursos Databind en XAML
- 22. databind de XAML al código detrás
- 23. databind con controles tabla muestra NotImplementedException
- 24. KnockoutJS - Databind a una colección de diccionario
- 25. .NET - Excel ListObject autosizing en databind
- 26. ¿Por qué es necesario el método DataBind()?
- 27. #define NULL NULL
- 28. WPF: TemplateBinding to StrokeThickness of Shape no funciona?
- 29. WPF - databinding to a property of same control
- 30. WPF binding ComboBox to enum (con un giro)
Excelente, gracias! –