Пытаюсь из MainWindow изменить текстблоки в UserControl, ночто-то идет не так

Вот функция из юзер контроля UserControl Class:

public void textBlockFilling(long id)
        {

            var selectedProducts = from p in db.Products
                                   where p.ProductID == id
                                   select p;

            IEnumerable<TypesOfProduct> type = GetType(id);
            textBlockName.Text = textBlockName.Text.Split(':')[0];
            textBlockName.Text += ": " + selectedProducts.First().ProductName;
}

MainWindow:

 void SearchString_OnSelect(object sender, SelectionChanged e)
        {
            if (e.Value != null)
            {
                Product product = e.Value as Product;

                ProductsView pa = new ProductsView();
                pa.textBlockFilling(product.ProductID);

            }
        }

Ничего не вышло. Увидел другой способ, делаю что-то не так: UC class:

  private delegate void NameCallBack(long varText);
        public void UpdateTextBox(long input)
        {
            db = new ApplicationContext();

            if (!Dispatcher.CheckAccess())
            {
                    textBlockName.Dispatcher.BeginInvoke(new NameCallBack(UpdateTextBox), new object[] { input });
            }
            else
            {
                if (db.Products == null) return;
                var selectedProducts = from p in db.Products
                                       where p.ProductID == input
                                       select p;
                textBlockName.Text = textBlockName.Text.Split(':')[0];
                textBlockName.Text =  ": " + selectedProducts.First().ProductName;
            }
        }

И MainWindow :

 public static readonly ProductsView ProductsLogWindow = new ProductsView();
        void SearchString_OnSelect(object sender, SelectionChanged e)
        {
            if (e.Value != null)
            {
                Product product = e.Value as Product;

                ProductsLogWindow.textBlockFilling(product.ProductID);
            }
        }

Не помогло, не понимаю что не так. enter image description here

Что обвел красным - часть где юзер контрол, они меняются по нажатию на радиобаттоны. Вот весь xaml главного окна MainWindow xaml:

<Window x:Class="AppProductAccounting.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:AppProductAccounting.MVVM.View"
        xmlns:viewModel="clr-namespace:AppProductAccounting.MVVM.ViewModel"
        xmlns:controls ="clr-namespace:AppProductAccounting.MVVM.View"
        mc:Ignorable="d"
        Height="600" Width="1120"
        WindowStyle="None"
        ResizeMode="NoResize"
        Background="Transparent"
        AllowsTransparency="True"
        Icon="images\diet.png">

    <Window.DataContext>
        <viewModel:MainViewModel/>
    </Window.DataContext>


    <Border Background="#272537"
            CornerRadius="20">
        <Grid>
            
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="200"/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="75"/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Image Source="Images\diet.png"
                VerticalAlignment="Center"
                       HorizontalAlignment="Center"/>
            <StackPanel Grid.Row="1">
                <RadioButton Content="Продукты"
                             Height="50"
                             Foreground="White"
                             FontSize="14"
                             Style="{StaticResource MenuButtonTheme}"
                             IsChecked="True"
                             Command="{Binding ProductsViewCommand}"
                             Checked="productsButton_Checked"
                             x:Name="productsButton"
                             />

                <RadioButton Content="Рецепты"
                             Height="50"
                             Foreground="White"
                             FontSize="14"
                             Style="{StaticResource MenuButtonTheme}"
                             Command="{Binding RecipesViewCommand}"
                             Checked="recipesButton_Checked"
                             x:Name="recipesButton"/>

                <RadioButton Content="Учет продуктов"
                             Height="50"
                             Foreground="White"
                             FontSize="14"
                             Style="{StaticResource MenuButtonTheme}"
                             Command="{Binding AccountingViewCommand}"
                             Checked="productsButton_Checked"
                             x:Name="accountingButton"/>

                <RadioButton Content="О программе"
                             Height="50"
                             Foreground="White"
                             FontSize="14"
                             Style="{StaticResource MenuButtonTheme}"/>

            </StackPanel>
            <Grid Margin="70,19,460,516" Grid.Column="1" Grid.RowSpan="2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="30"></RowDefinition>
                    <RowDefinition Height="35"></RowDefinition>
                    <RowDefinition Height="35"></RowDefinition>
                    <RowDefinition Height="35"></RowDefinition>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100"></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <controls:SearchString 
                    x:Name="FString" Grid.ColumnSpan="2"
                    
                    HorizontalAlignment="Right"
                    Height="30" VerticalAlignment="Center" Width="190"
                    OnSelect="SearchString_OnSelect"/>

            </Grid>
            <Button Height="25" Width="25" Grid.Column="1" Margin="868,12,0,37" HorizontalAlignment="Left">
                <Button.Template>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="Bd" Background="Transparent">
                            <Path x:Name="Pt" Data="M0,0 M0.2,0.2 L 0.8,0.8 M0.2,0.8 L0.8,0.2 M1,1"
                      Stretch="Uniform" Stroke="Black" StrokeThickness="2"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="Bd" Property="Background" Value="#22202f"/>
                                <Setter TargetName="Pt" Property="Stroke" Value="White"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Button.Template>
            </Button>
            <ContentControl Grid.Row="1"
                            Grid.Column="1"
                            Margin="10"
                            Content="{Binding CurrentView}"
                            />
            


        </Grid>
    </Border>
</Window>

Предложили впихнуть вот это и чутка поправить функцию:

<Window ...>
    <Grid>
        <controls:ProductsView Name="productView"/>
    </Grid>
</Window>

Но начинает выдавать ошибки. Спасайте через 4 часа работу сдавать а я в wpf вообще не шарю


Ответы (0 шт):