2015-06-12 15 views
6
訪問按鈕

我有一個數據網格下面的.xaml和希望顯示在的.cs代碼的.xaml代碼的某些條件/隱藏按鈕,如下如何從DataGridTemplateColumn.CellTemplate

<DataGridTemplateColumn Header="Action" Width="auto" > 
    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <Button x:Name="btnConfirm" Content="Confirm" Click="ConfirmButton_Click" Height="auto" Width="auto" Opacity="100" Background="Transparent" BorderBrush="Transparent" HorizontalAlignment="Left"/> 
       <Button x:Name="btnDecline" Content="Decline" Click="btnDecline_Click" Height="auto" Width="auto" Opacity="100" Background="Transparent" BorderBrush="Transparent" HorizontalAlignment="Left" /> 
       <Button x:Name="btnCancel" Content="Cancel" Click="btnCancel_Click" Height="auto" Width="auto" Opacity="100" Background="Transparent" BorderBrush="Transparent" HorizontalAlignment="Left"/> 
      </StackPanel> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn> 

和的.cs代碼是

foreach (sp_ProcessingJobsResult item in grdUnConfirmJobs.ItemsSource) 
{ 
var row = grdUnConfirmJobs.ItemContainerGenerator.ContainerFromItem(item) as System.Windows.Controls.DataGridRow; 
if (item.Status == "Cancellation Requested.") 
    { 
     //how find control  
    } 
} 
+0

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

+0

http://stackoverflow.com/questions/19379946/how-to-access-a-control-within-data-template-from-code-behind的可能重複 – Ahmad

回答

0

你必須使用綁定爲此。所以,讓我們快速的方法:

這裏是後面這個代碼:

public partial class MainWindow : Window 
{ 
    public bool ButtonIsVisible 
    { 
     get { return (bool)GetValue(ButtonIsVisibleProperty); } 
     set { SetValue(ButtonIsVisibleProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for ButtonIsVisible. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty ButtonIsVisibleProperty = 
     DependencyProperty.Register("ButtonIsVisible", typeof(bool), typeof(MainWindow), new UIPropertyMetadata(true)); 

    public ObservableCollection<Person> items { get; set; } 

    public MainWindow() 
    { 
     InitializeComponent(); 
     items = new ObservableCollection<Person>(); 
     items.Add(new Person() { Name = "FirstName" }); 
     items.Add(new Person() { Name = "SecondName" }); 

     this.DataContext = this; 
    } 
} 

這是我的例子型號:

public class Person : INotifyPropertyChanged 
{ 
    private string _Name; 

    public string Name 
    { 
     get { return _Name; } 
     set 
     { 
      _Name = value; 
      PropertyChanged(this, new PropertyChangedEventArgs("Name")); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged = delegate { }; 
} 

您的知名度不是布爾類型,因此我們需要一個轉換器,用於此:

public class BoolToVis : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     var isVisible = (bool)value; 

     return isVisible ? Visibility.Visible : Visibility.Hidden; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

這裏是整個XAML代碼:

<Window x:Class="DataGridCellsBackground.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525" 
    xmlns:local="clr-namespace:DataGridCellsBackground"  > 
<Grid> 
    <Grid.Resources> 
     <local:BoolToVis x:Key="BoolTovisibilityConverter"/> 
    </Grid.Resources> 
    <DataGrid SelectionUnit="Cell" 
       ItemsSource="{Binding items}" 
       AutoGenerateColumns="False"> 
     <DataGrid.Resources> 
     </DataGrid.Resources> 
     <DataGrid.Columns> 
      <DataGridTemplateColumn> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding Name}"/> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
      <DataGridTemplateColumn> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <Button Content="Visible" Visibility="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Path=DataContext.ButtonIsVisible, Converter={StaticResource BoolTovisibilityConverter}}"/> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
     </DataGrid.Columns> 
    </DataGrid> 
</Grid> 

實際上你看到的按鈕能見度一個奇怪的綁定語法。這是爲什麼?我假設你不需要模型中的這個功能,所以我回到了DataGrid的DataContext,以達到該DependencyProperty。我並不完全瞭解你的情況,但我已經向你展示了一些易於使用的機制。