I'm bind a Telerik RadGridView to a List<MyObject> myList = new List&l开发者_如何学Ct;MyObject>
. But if the myList.Count == 0
(the list is empty ;) ) I want to show another control to the user.
I know I could use some visibility converter, but I prefer achieving this in XAML.
Thank you
I think that value converters are your only choice here :) However, I've found out that if you structure them properly, value converters are great.
Here are a couple of good tools for this:
- A Generic Boolean Value Converter
- Linking Multiple Value Converters in WPF and Silverlight
With these tools in mind, I would go with something like this:
<Grid>
<telerik:RadGridView ItemsSource="{Binding myList}">
<telerik:RadGridView.Visibility>
<Binding Path="myList">
<Binding.Converter>
<converters:SequentialValueConverter>
<converters:IsEmptyConverter />
<converters:BooleanToVisibilityConverter TrueValue="Collapsed" FalseValue="Visible" />
</converters:SequentialValueConverter>
</Binding.Converter>
</Binding>
</telerik:RadGridView.Visibility>
</telerik:RadGridView>
<YourControl>
<YourControl.Visibility>
<Binding Path="myList">
<Binding.Converter>
<converters:SequentialValueConverter>
<converters:IsEmptyConverter />
<converters:BooleanToVisibilityConverter TrueValue="Visible" FalseValue="Collapsed" />
</converters:SequentialValueConverter>
</Binding.Converter>
</Binding>
</YourControl.Visibility>
</YourControl>
</Grid>
Also, as Jason said, myList needs to be an ObservableCollection so the gui gets notified when it changes.
Hope it helps!
If you switched to ObservableCollection<MyObject>
you can bind using VisibilityConverters to that your myList.Count
all in XAML. If you are having issues because you are setting the ItemsSource in codebehind, you may want to have it be a resource or switch to something more MVVM.
精彩评论