我有一個承載 UserControl 的 WPF 頁面。UserControl 有一個 Button,它可以執行一些操作,并最終想要更改系結到頁面上 DataGrid 的 List。不幸的是,我無法訪問此串列/資料網格。似乎頁面可以從托管的 UserControl 訪問所有公共變數,但 UserControl 無法從頁面訪問任何變數......有人可以幫我嗎?
這是我頁面的 xaml:
<Page x:Class="SchrauberDB.View.Werkzeug_Window"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:SchrauberDB.Controls"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
Title="Werkzeug_Window"
mc:Ignorable="d"
FontFamily="../Fonts/Poppins/#Poppins"
d:DesignWidth="1400" d:DesignHeight="950"
>
<StackPanel
Orientation="Horizontal"
VerticalAlignment="Center"
HorizontalAlignment="Right"
Margin="0,0,0,0.2" Width="404"
>
<Button
Height="45" Width="150"
Margin="20,0,0,0"
Background="{x:Null}"
Click="FilterMaske_Click"
>
<StackPanel Margin="0,0,0,0" HorizontalAlignment="Left" Width="90">
<Image Height="30" Width="30" HorizontalAlignment="Left" Source="Assets\filter-outline_black.png" />
<Label Content="Filter" HorizontalAlignment="Center" Margin="20,-30,0,0" FontSize="14" Height="30"/>
</StackPanel>
</Button>
</StackPanel>
<DataGrid x:Name="DataGrid"
AlternatingRowBackground="#F8F8F8" Margin="40,60,40,45"
Grid.Column="2" Grid.ColumnSpan="10"
Grid.Row="1" Grid.RowSpan="12"
RowHeight="47"
ColumnHeaderHeight="47"
Padding="0"
FontSize="18"
ColumnWidth="150"
HorizontalScrollBarVisibility="Visible"
AutoGenerateColumns ="False"
>
<DataGrid.Columns >
<DataGridTextColumn x:Name="filter" Header="Filter" FontSize="18" IsReadOnly="True" Binding="{Binding Path=filter}"></DataGridTextColumn>
<DataGridTextColumn x:Name="bemiNr" Header="Bemi Nr" FontSize="18" IsReadOnly="True" Binding="{Binding Path=bemiNr}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<local:FilterWerkzeug x:Name="Filtermaske" Padding="10" Visibility="Collapsed"></local:FilterWerkzeug>
</Grid>
在頁面背后的代碼中,我有一個系結到 DataGrid 的串列:
public List<DataModel.Werkzeug> werkzeuge = new List<DataModel.Werkzeug>();
//Some logic...
DataGrid.ItemsSource = werkzeuge;
在 UserControl 背后的代碼中,我有一個最終想要更新 List werkzeuge 的按鈕
這是 UserControl 的 xaml:
<UserControl x:Class="SchrauberDB.Controls.FilterWerkzeug"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:SchrauberDB.Controls"
mc:Ignorable="d"
d:DesignHeight="200" d:DesignWidth="1100"
FontFamily="../Fonts/Poppins/#Poppins">
<Grid Background="#FEFFFF" >
<StackPanel Grid.Row="0" Grid.Column="0">
<TextBlock>filter</TextBlock>
<ComboBox x:Name="cbFilter" Width="150"/>
</StackPanel>
<Button Height="45" Width="130" Margin="17.6,14.4,10,7" Click="Button_Click">
<TextBlock FontSize="14" FontWeight="Medium">
Filter
</TextBlock>
</Button>
</Grid>
</UserControl>
uj5u.com熱心網友回復:
這是一個非常簡單的示例:
- 頁面資料背景關系是
Model具有公共串列的物件 - 頁面顯示該串列
- 頁面顯示用戶控制元件
- 這個用戶控制元件有一個按鈕
- 該按鈕更新串列中的
Model

該模型:
using System.Collections.ObjectModel;
namespace WpfApp1
{
public class Model
{
public ObservableCollection<string> Strings { get; } = new();
}
}
用戶控制元件 XAML:
<UserControl x:Class="WpfApp1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:wpfApp1="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance wpfApp1:Model}"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Button Content="Add" Click="ButtonBase_OnClick" />
</Grid>
</UserControl>
用戶控制代碼:
using System;
using System.Windows;
namespace WpfApp1
{
public partial class UserControl1
{
public UserControl1()
{
InitializeComponent();
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
var model = DataContext as Model ?? throw new InvalidOperationException();
model.Strings.Add(DateTime.Now.ToString("O"));
}
}
}
頁面 XAML:
<Page x:Class="WpfApp1.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Page1"
d:DataContext="{d:DesignInstance local:Model}">
<Page.DataContext>
<local:Model />
</Page.DataContext>
<StackPanel>
<local:UserControl1 />
<ItemsControl ItemsSource="{Binding Strings}" />
</StackPanel>
</Page>
頁面代碼:
namespace WpfApp1
{
public partial class Page1
{
public Page1()
{
InitializeComponent();
}
}
}
視窗 XAML:
<NavigationWindow x:Class="WpfApp1.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"
mc:Ignorable="d"
Source="Page1.xaml">
</NavigationWindow>
視窗代碼:
namespace WpfApp1
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
}
}
uj5u.com熱心網友回復:
您可以Page在可視化樹中找到父級,然后DataGrid直接訪問:
private void Button_Click(object sender, RoutedEventArgs e)
{
Werkzeug_Window page = FindParent<Werkzeug_Window>(this);
page.DataGrid = ...;
}
private static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
{
var parent = VisualTreeHelper.GetParent(dependencyObject);
if (parent == null) return null;
var parentT = parent as T;
return parentT ?? FindParent<T>(parent);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/330487.html
