Brak dostepu do kontrolki (XAML C#)

0

Witam, mam taki kod w xaml

                    <DataGridTemplateColumn Header="Status" Width="120">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <ComboBox x:Name="_status" Text="{Binding status}" />
                                </StackPanel>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

Ktos moze mi wyjasnic czemu nie mam dostepu do ComboBox _status ?
Chce uzyc tego jako _status.ItemsSource ale wywala blad:

Error 1 The name '_status' does not exist in the current context

0

Controls inside a template are in a different name scope, so you can't access it in code-behind via its name.

Źródło
Możesz też poczytać tu.
Ale po co chcesz to robić?
Ok, doczytałem. ItemsSource możesz ustawić w xamlu.

0

tylko ze u mnie to jest generowane przez metode innej klasy, w tym jest problem. W c# wiem jak sie odwolac ale niestety nie mam dostepu do niej

0

Co jest generowane przez inną metodę?

0

ItemsSource to metoda w innej klasie.
Ta metoda generuje mi liste ktora chcialbym by byla w tym ComboBox.
Jesli dam sam ComboBox x:Name"_status" to dziala dobrze, jak juz wrzyce do datatemplate nie moge sie odwolac do tej kontrolki

0

Zamień ItemsSource na property i zbinduj.

0

tzn?

0
                            <DataTemplate>
                                <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.ComboBoxItems}" Foreground="Blue" Width="85"  />
                            </DataTemplate>

W code behind

public MainWindow()
{
     InitializeComponent();
     this.DataContext = this;
     ComboBoxItems = new ObservableCollection<string>()
     {
         "sddsdf","sdf"
      };
}
public ObservableCollection<string> ComboBoxItems { get;set;}

Property ComboBoxItems możesz sobie uzupełnić skąd chcesz.

0

tez nie dziala, na razie chyba zostawie to i lece dalej, wroce do tego pozniej, moze gdzies sie doszkole to dorzuce rozwiazanie

0

Najlepiej jak byś pokazał więcej kodu, bo nie wiem jak to masz zrobione więc mój kod wklejony na żywca może nie działać.

1 użytkowników online, w tym zalogowanych: 0, gości: 1