通过 XAML 字符串创建时将事件设置为按钮
Set event to button when created via XAML string
我得到了以下代码,将是一个动态生成的网格:
string xaml = "<Grid Margin='8 8 0 0' Width='175'>" +
"<Grid.RowDefinitions>" +
"<RowDefinition Height='115'/>" +
"<RowDefinition Height='*'/>" +
"<RowDefinition Height='Auto'/>" +
"</Grid.RowDefinitions>" +
"<Image Source='C:/Users/SaFteiNZz/Curro/yo2.png' Height='115' Width='171' Stretch='Uniform'/>" +
"<Button x:Name='BtnEditAc' Grid.Row='0' Style='{StaticResource MaterialDesignFloatingActionMiniAccentButton}' HorizontalAlignment='Right' VerticalAlignment='Bottom' Margin='0,0,16,-20'>" +
"<materialDesign:PackIcon Kind='AccountSettingsVariant' Width='30' Height='30' Margin='0,0,0,5'/>" +
"</Button>" +
"<StackPanel Grid.Row='1' Margin='8,24,8,8'>" +
"<TextBlock TextWrapping='Wrap' FontWeight='Bold'>Pablo Antonio Hernandez Hernandez</TextBlock>" +
"<TextBlock TextWrapping='Wrap' VerticalAlignment='Center'>46080696N</TextBlock>" +
"</StackPanel>" +
"<Border BorderBrush='White' BorderThickness='1' CornerRadius='0' Grid.Row='0' Grid.RowSpan='3'>" +
"<Border.Effect>" +
"<DropShadowEffect BlurRadius='10' Direction='-90' Color='Black' ShadowDepth='0'/>" +
"</Border.Effect>" +
"</Border>" +
"</Grid>";
UIElement element = (UIElement)XamlReader.Parse(xaml, context);
empDisplay.Children.Add(element);
这样代码就可以了。
但我想设置点击事件到按钮(BtnEditAc)如果我直接在 XAML 字符串中执行操作会引发错误(无法从文本 BtnEditAc_Click 或类似内容创建点击)。
有没有办法为该按钮设置点击事件?或者以某种方式连接到一个函数?
我将使用编码的 CLR 对象而不是 XamlReader 来实现它,但首先我想知道是否有解决方案。
希望你知道我做错了什么。
让我们看看这个解决方案是否可行:
//Here you have the grid
Grid element = (Grid)XamlReader.Parse(xaml, context);
//Now try to get the button
Button btn = (Button)element.FindName("BtnEditAc");
if(btn != null)
{
btn.Click += new RoutedEventHandler(OnBtnEditAcClick);
}
其中 OnBtnEditAcClick
是事件处理程序。
然后将元素添加到您的主视图:
empDisplay.Children.Add(element);
希望对你有所帮助。
您可以从 xml 而不是 Click 事件解析命令绑定:
<Button x:Name='BtnEditAc' Command='{Binding MyCommand}' Grid.Row='0' ...
如果由于某些原因你不想使用命令,你可以在代码中手动添加点击事件,只要你能在刚刚解析的xaml
元素中找到Button
。例如,如果您知道序列化 xaml 中的根元素将是 Grid
:
Grid rootElement = (Grid)XamlReader.Parse(xaml, context);
grdMain.Children.Add(rootElement);
Button btn = (Button)rootElement.FindName("BtnEditAc");
btn.Click += btn_Click;
或者:
UIElement elem = (UIElement)XamlReader.Parse(xaml, context);
Button b =(Button) LogicalTreeHelper.FindLogicalNode(elem, "BtnEditAc");
作为替代方案,如果您不知道根元素的类型,您可以使用 this code 按名称和类型查找它:
public static T FindChild<T>(this DependencyObject parent, string childName)
where T : DependencyObject
{
// Confirm parent and childName are valid.
if (parent == null) return null;
T foundChild = null;
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
// If the child is not of the request child type child
T childType = child as T;
if (childType == null)
{
// recursively drill down the tree
foundChild = FindChild<T>(child, childName);
// If the child is found, break so we do not overwrite the found child.
if (foundChild != null) break;
}
else if (!string.IsNullOrEmpty(childName))
{
var frameworkElement = child as FrameworkElement;
// If the child's name is set for search
if (frameworkElement != null && frameworkElement.Name == childName)
{
// if the child's name is of the request name
foundChild = (T)child;
break;
}
}
else
{
// child element found.
foundChild = (T)child;
break;
}
}
return foundChild;
}
或者如果您不知道按钮的名称,您可以使用 this extension method:
public static T GetChildOfType<T>(this DependencyObject depObj) where T : DependencyObject
{
if (depObj == null) return null;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
var child = VisualTreeHelper.GetChild(depObj, i);
var result = (child as T) ?? GetChildOfType<T>(child);
if (result != null) return result;
}
return null;
}
如果你想在xaml中设置你的事件处理程序并手动解析它,我不知道是否可以完成。
我得到了以下代码,将是一个动态生成的网格:
string xaml = "<Grid Margin='8 8 0 0' Width='175'>" +
"<Grid.RowDefinitions>" +
"<RowDefinition Height='115'/>" +
"<RowDefinition Height='*'/>" +
"<RowDefinition Height='Auto'/>" +
"</Grid.RowDefinitions>" +
"<Image Source='C:/Users/SaFteiNZz/Curro/yo2.png' Height='115' Width='171' Stretch='Uniform'/>" +
"<Button x:Name='BtnEditAc' Grid.Row='0' Style='{StaticResource MaterialDesignFloatingActionMiniAccentButton}' HorizontalAlignment='Right' VerticalAlignment='Bottom' Margin='0,0,16,-20'>" +
"<materialDesign:PackIcon Kind='AccountSettingsVariant' Width='30' Height='30' Margin='0,0,0,5'/>" +
"</Button>" +
"<StackPanel Grid.Row='1' Margin='8,24,8,8'>" +
"<TextBlock TextWrapping='Wrap' FontWeight='Bold'>Pablo Antonio Hernandez Hernandez</TextBlock>" +
"<TextBlock TextWrapping='Wrap' VerticalAlignment='Center'>46080696N</TextBlock>" +
"</StackPanel>" +
"<Border BorderBrush='White' BorderThickness='1' CornerRadius='0' Grid.Row='0' Grid.RowSpan='3'>" +
"<Border.Effect>" +
"<DropShadowEffect BlurRadius='10' Direction='-90' Color='Black' ShadowDepth='0'/>" +
"</Border.Effect>" +
"</Border>" +
"</Grid>";
UIElement element = (UIElement)XamlReader.Parse(xaml, context);
empDisplay.Children.Add(element);
这样代码就可以了。
但我想设置点击事件到按钮(BtnEditAc)如果我直接在 XAML 字符串中执行操作会引发错误(无法从文本 BtnEditAc_Click 或类似内容创建点击)。
有没有办法为该按钮设置点击事件?或者以某种方式连接到一个函数?
我将使用编码的 CLR 对象而不是 XamlReader 来实现它,但首先我想知道是否有解决方案。
希望你知道我做错了什么。
让我们看看这个解决方案是否可行:
//Here you have the grid
Grid element = (Grid)XamlReader.Parse(xaml, context);
//Now try to get the button
Button btn = (Button)element.FindName("BtnEditAc");
if(btn != null)
{
btn.Click += new RoutedEventHandler(OnBtnEditAcClick);
}
其中 OnBtnEditAcClick
是事件处理程序。
然后将元素添加到您的主视图:
empDisplay.Children.Add(element);
希望对你有所帮助。
您可以从 xml 而不是 Click 事件解析命令绑定:
<Button x:Name='BtnEditAc' Command='{Binding MyCommand}' Grid.Row='0' ...
如果由于某些原因你不想使用命令,你可以在代码中手动添加点击事件,只要你能在刚刚解析的xaml
元素中找到Button
。例如,如果您知道序列化 xaml 中的根元素将是 Grid
:
Grid rootElement = (Grid)XamlReader.Parse(xaml, context);
grdMain.Children.Add(rootElement);
Button btn = (Button)rootElement.FindName("BtnEditAc");
btn.Click += btn_Click;
或者:
UIElement elem = (UIElement)XamlReader.Parse(xaml, context);
Button b =(Button) LogicalTreeHelper.FindLogicalNode(elem, "BtnEditAc");
作为替代方案,如果您不知道根元素的类型,您可以使用 this code 按名称和类型查找它:
public static T FindChild<T>(this DependencyObject parent, string childName)
where T : DependencyObject
{
// Confirm parent and childName are valid.
if (parent == null) return null;
T foundChild = null;
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
// If the child is not of the request child type child
T childType = child as T;
if (childType == null)
{
// recursively drill down the tree
foundChild = FindChild<T>(child, childName);
// If the child is found, break so we do not overwrite the found child.
if (foundChild != null) break;
}
else if (!string.IsNullOrEmpty(childName))
{
var frameworkElement = child as FrameworkElement;
// If the child's name is set for search
if (frameworkElement != null && frameworkElement.Name == childName)
{
// if the child's name is of the request name
foundChild = (T)child;
break;
}
}
else
{
// child element found.
foundChild = (T)child;
break;
}
}
return foundChild;
}
或者如果您不知道按钮的名称,您可以使用 this extension method:
public static T GetChildOfType<T>(this DependencyObject depObj) where T : DependencyObject
{
if (depObj == null) return null;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
var child = VisualTreeHelper.GetChild(depObj, i);
var result = (child as T) ?? GetChildOfType<T>(child);
if (result != null) return result;
}
return null;
}
如果你想在xaml中设置你的事件处理程序并手动解析它,我不知道是否可以完成。