Dont display the button if any value exist in quantity
Goal:
The button named "btnReturn" should not be displayed if the value of the quantity is 0 and minus.Problem:
Have considered alot and I can't find a solution to make the button not to be invisible. It can be a XAML code or C#.Please remember that you have lots of product to be displayed and I also used datacontext to sync between XAML and list.
private void UpdateGUI(int pSaleId)
{
lstRepurchase.DataContext = _myManagerProduct_SaleAndProductQuantity.DisplaySoldProductInTheRepurchaseListView(pSaleId);
}
<ListView Canvas.Left="8" Canvas.Top="49.494" ItemsSource="{Binding}" SelectionMode="Single" Margin="236.78,17.48,8,44.707" Name="lstRepurchase">
<ListView.View>
<GridView>
<GridViewColumn Header="Article Number" Width="auto" DisplayMemberBinding="{Binding Path=_articleNumber}" Te开发者_开发知识库xtBlock.TextAlignment="Left" />
<GridViewColumn Header="Name" Width="auto" DisplayMemberBinding="{Binding Path=_name}" TextBlock.TextAlignment="Left" />
<GridViewColumn Header="Quantity" Width="auto" DisplayMemberBinding="{Binding Path=_quantity}" TextBlock.TextAlignment="Left" />
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Name="btnReturn" MinHeight="20" MinWidth="50" Content="Delete" Click="btnReturn_Click" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Here is a converter for Int32 to Visability. Now the syntax for passing the count to the convert could need to bind to the ItemSource for count.
[ValueConversion(typeof(Int32), typeof(Visibility))]
public class IntVisabilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Int32 inCount = (Int32)value;
if (inCount > 0) return Visibility.Visible;
else return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return true;
}
}
XAML
<xmlns:local="clr-namespace:yourNameSpace"
<local:VisabilityConverter x:Key="visabilityConverter"/>
Visibility="{Binding Path=Count, Converter={StaticResource visabilityConverter}
You will need to read up for where those lines go but they are all on the XAML.
You will need to bind the Visibility property to the _quantity property and use a converter which takes in the quantity and returns a visibility.
Here is some information regarding converters:
http://www.codeproject.com/KB/WPF/OppositeBoolConverter.aspx
So you want the button to change Visibility when 'quantity' is zero or less. Try messing around with the button's visible or visibility property. This C# code will make a Windows Form button invisible:
if(quantity <=0)
btnReturn.Visibile = false;
Consider using Boolean-to-Visibility Converter, see an example of custom and flexible converter here - BooleanToVisibilityConverter by Kent Boogaart. Or use built-in BooleanToVisibilityConverter Class
1) Define boolean property like
public bool IsQuantityGreaterThenZero
{
get
{
return this.quantity > 0;
}
}
2) Update buttond definition in XAML to use binding with converter in place:
<Button Visibility="{Binding IsQuantityGreaterThenZero,
Converter={StaticResource yourConverter}}"/>
Don't reinvent the wheel. The Boolean to Visibility converter is native in WPF:
http://msdn.microsoft.com/en-us/library/system.windows.controls.booleantovisibilityconverter.aspx
精彩评论