2015-06-12 2 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 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 { }; 
} 

आपका दृश्यता एक bool प्रकार ऐसा नहीं है:

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 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> 

दरअसल आप बटन दृश्यता में एक अजीब बाध्यकारी वाक्य रचना को देखते हैं। ऐसा क्यों है? मुझे लगता है कि आपको मॉडल में इस कार्यक्षमता की आवश्यकता नहीं है, इसलिए मैं उस निर्भरता प्रॉपर्टी तक पहुंचने के लिए डेटाग्रिड के डेटाकॉन्टेक्स्ट पर वापस आ गया। मैं आपके परिदृश्य के बारे में पूरी तरह से निश्चित नहीं हूं लेकिन मैंने आपको आसानी से उपयोग करने के लिए कुछ तंत्र दिखाए हैं।

संबंधित मुद्दे