2013-03-12 132 views
1
<Window x:Class="WPfDataGrid.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="450" Width="525" Loaded="Window_Loaded"> 
    <Grid> 
      <DataGrid x:Name="dgrdEmployee" Width="300" Height="300" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"/> 
      <Button Content="Navigation" x:Name="BtnNavigation" Width="90" Height="40" Margin="204,336,209,-15" /> 
    </Grid> 
</Window> 

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 

    } 

    public class Employees 
    { 
     public string Name { get; set; } 
     public string Id { get; set; } 
     public string Address { get; set; } 
    } 

    public List<Employees> EmployeeList() 
    { 
     XDocument employees = XDocument.Load(@"C:\Employees.xml"); 
     List<Employees> employee = (from em in employees.Descendants("Employee") 
            select new Employees 
            { 
             Name = em.Element("Name").Value, 
             Id = em.Element("Id").Value, 
             Address = em.Element("Address").Value 
            }).ToList(); 
     return employee; 
    } 
private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     dgrdEmployee.ItemsSource = EmployeeList(); 
    } 
} 

以上代碼,我得到以下結果。如何將wpf datagrid scrollviewer visiblity綁定到按鈕可見性?

enter image description here

如果數據網格滾動查看器中顯示的按鈕應該是可見的,否則按鈕應該是崩潰。有沒有改變這樣做?

我試過下面的東西。但對我來說這沒有意義。

<Button Content="Navigation" x:Name="BtnNavigation" Visibility="{Binding Visibility, ElementName=dgrdEmployee.ScrollViewer}" Width="90" Height="40" Margin="204,336,209,-15" /> 

回答

0

ScrollViewer始終可見。 ScrollViewer將顯示或隱藏其滾動條,具體取決於顯示的數據量。

假設你可以獲取到ScrollViewerVerticalScrollBarVisiblityHorizontalScrollBarVisiblity性能,你也許可以創建這些屬性和按鈕的Visibliity屬性之間的MultiBinding。您可能還必須創建一個value converter,因爲滾動條可見性屬性使用不同的類型(除標準可見性值以外,還包括Auto值)。

+0

我不知道請幫我用代碼。 – 2013-03-12 12:09:47