gpt4 book ai didi

c# - 将 DataTemplate 中的 WPF RelayCommand 绑定(bind)到 UserControl 内的按钮

转载 作者:行者123 更新时间:2023-12-03 10:41:43 25 4
gpt4 key购买 nike

为了解决这个问题,我搜索了很长时间 RelayCommands但找不到类似的解决方案。

问题是我有一个 UserControl , 在这个 UserConrol是一个按钮( btcNotKnown 在代码末尾):

<UserControl x:Class        = "Vokabelizer.Views.viewLearnSpeak"
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:Vokabelizer.Views"
xmlns:conv = "clr-namespace:Vokabelizer.Global.Converter"
xmlns:ccont = "clr-namespace:Vokabelizer.Controls;assembly=Vokabelizer.Controls"
mc:Ignorable = "d"
d:DesignHeight = "300"
d:DesignWidth = "800"
Height = "300"
x:Name = "root">

<UserControl.Resources>
...
</UserControl.Resources>

<Grid Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="0">

<Grid.RowDefinitions>
...
</Grid.RowDefinitions>

<!-- The question to answer -->
<Border Grid.Column = "0"
Grid.Row = "0"
Padding = "5"
HorizontalAlignment = "Stretch"
VerticalAlignment = "Stretch">

<TextBlock Text = "{Binding Path=LessonNative}"
Style = "{StaticResource UIText}"/>

</Border>

<!-- Validation content -->
<Border Grid.Column = "0"
Grid.Row = "1"
Padding = "5">

<ToggleButton x:Name = "QnAToggle"
Command = "{Binding Path=cmdValidateOK}"
IsThreeState = "False">

<ToggleButton.Style>
<Style TargetType="ToggleButton" BasedOn="{StaticResource ValidateToggle}">
<Style.Triggers>
<Trigger Property="IsChecked" Value="False">
<Setter Property="Content">
<Setter.Value>
<TextBlock Text = "?"
Style = "{StaticResource UIText}" />
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content">
<Setter.Value>
<TextBlock Text = "{Binding Path=LessonForeign}"
Style = "{StaticResource UIText}" />
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>

</ToggleButton>

</Border>

<!-- Result Evaluation buttons -->
<Border Grid.Column = "0"
Grid.Row = "2">

<Grid>

<Grid.ColumnDefinitions>
...
</Grid.ColumnDefinitions>

<ccont:vokButton x:Name = "btcNotKnown"
Command = "{Binding Command, ElementName=root}"
Grid.Column = "0"
Grid.Row = "0"
Content = "Not Known"
Corner = "5"
Style = "{StaticResource ValidateNotKnown}"
Visibility = "{Binding ElementName=QnAToggle, Path=IsChecked, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=hidden}" />

</Grid>

</Border>

</Grid>

UserControl 后面的代码中我定义了一个 DependencyPropertyUserControl 公开命令并因此绑定(bind)到 UserControls 中的 Button xml:
public partial class viewLearnSpeak : UserControl
{
public viewLearnSpeak()
{
InitializeComponent();
}

#region DepProp: Command

public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(viewLearnSpeak), new PropertyMetadata(null));

public ICommand Command
{
get => (ICommand)GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}

#endregion
}

此用户控件现在在 DataTemplate 内的 Window 中使用,这就是问题的开始:
<DataTemplate DataType="{x:Type vm:vmLearnSpeak}">
<local:viewLearnSpeak Command="{Binding cmdVMNotKnown, ElementName=root}" />
</DataTemplate>

Window (View) 绑定(bind)到 ViewModel (vmSession),该 ViewModel (vmSession) 应该承载命令代码,以便 DataTemplate 使用的任何 CustomControl , 有效 Customcontrol可以将其命令绑定(bind)到 vmSession ViewModel 中的操作(所有 CustomControls 将在单击它们所托管的特定按钮时执行相同的操作)。

代码后面的命令定义:
#region Command: Not Known

private ICommand _cmdVMNotKnown;
public ICommand cmdVMNotKnown
{
get
{
if (_cmdVMNotKnown == null)
{
_cmdVMNotKnown = new RelayCommand(
param => this.doVMNotKnown(),
param => { return true; }
);
}
return _cmdVMNotKnown;
}
}

protected void doVMNotKnown()
{
}

#endregion

不幸的是,我只是可以让绑定(bind)以这种方式工作,而且我没有关于如何将按钮命令绑定(bind)到 UserControl 后面的 Viewmodel 而不是绑定(bind)到以 MVVM 方式托管 Usercontrols ViewModel 的 Viewmodel 的线索,而无需依赖委托(delegate)或其他处理程序...

这是完整的窗口 XAML(使用 DataTemplate 的 ContentControl 位于末尾):
<Window x:Class         = "Vokabelizer.Views.wndSession"
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:Vokabelizer.Views"
xmlns:res = "clr-namespace:Vokabelizer.Resources"
xmlns:vm = "clr-namespace:Vokabelizer.ViewModels"
mc:Ignorable = "d"
Title = "{x:Static res:Strings.wndLearnTitle}"
Height = "410"
Width = "800"
ResizeMode = "NoResize"
x:Name = "root">

<Window.DataContext>
<vm:vmSession />
</Window.DataContext>

<Window.Resources>
<DataTemplate DataType="{x:Type vm:vmLearnSpeak}">
<local:viewLearnSpeak Command="{Binding cmdVMNotKnown, ElementName=root}" />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:vmLearnWrite}">
<local:viewLearnWrite />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:vmLearnListen}">
<local:viewLearnListen />
</DataTemplate>
</Window.Resources>

<Grid Margin="10">

<Grid.ColumnDefinitions>
<ColumnDefinition Width = "Auto" />
<ColumnDefinition Width = "*" />
<ColumnDefinition Width = "Auto" />
</Grid.ColumnDefinitions>

<Grid.RowDefinitions>
<RowDefinition Height = "50" />
<RowDefinition Height = "300" />
</Grid.RowDefinitions>

<!-- Title description -->
<TextBlock x:Name="txtModeDescription" Text="{Binding LearnTitle}"
HorizontalAlignment="Left" VerticalAlignment="Center"
Grid.Column="0" Grid.ColumnSpan="1"
Grid.Row="0" Grid.RowSpan="1"
Margin="0, 0, 30, 0" Foreground="DarkGray"
FontWeight="Bold" FontSize="18" FontFamily="Arial Black" />

<!-- Progress Slider -->
<Slider x:Name="sliderProgress"
HorizontalAlignment="Stretch" VerticalAlignment="Center"
Grid.Column="1" Grid.ColumnSpan="1"
Grid.Row="0" Grid.RowSpan="1"
Minimum="0" Maximum="11" />

<!-- Title status -->
<TextBlock x:Name="txtBatchAdvancement" Text="{Binding LearnProgressBatch}"
HorizontalAlignment = "Right" VerticalAlignment = "Center"
Grid.Column = "3" Grid.ColumnSpan = "1"
Grid.Row = "0" Grid.RowSpan = "1"
Margin = "10, 0, 0, 0"/>

<Border Grid.Row = "1"
Grid.Column = "0"
HorizontalAlignment = "Center"
VerticalAlignment = "Center"
Padding = "0">

<Image Source = "Demo.png"
Height = "300"
Width = "300"
HorizontalAlignment = "Center"
VerticalAlignment = "Center" />

</Border>

<ContentControl Content = "{Binding learningViewModel}"
Grid.Row = "1" Grid.RowSpan="1"
Grid.Column = "1" Grid.ColumnSpan="2"
Height = "300"
Margin = "20, 0, 20, 0"/>

</Grid>

这里是从 Window ViewModel 中为 DataTemplate 公开 UserControl ViewModel 的部分:
private ILearnVM _learningViewModel;
/// <summary>
/// The Learning Provider View Model
/// </summary>
public ILearnVM learningViewModel
{
get => this._learningViewModel;
private set
{
this._learningViewModel = value;

this.OnPropertyChanged(nameof(this.learningViewModel));
}
}

最佳答案

好吧,我必须非常感谢@ΩmegaMan 和其他要求一些代码才能工作的人。

我建立了一个真正有助于调试问题的小型玩具项目。

我改变了什么?其实很简单,在DataTemplate里面,我只需要使用一个指向Window的相对源,并使用一个DataContext引用Window ViewModel的command属性(在Window Datacontext中引用)。

我最终改变了:

<Window.Resources>
<DataTemplate DataType="{x:Type vm:vmLearnSpeak}">
<local:viewLearnSpeak Command="{Binding cmdVMNotKnown, ElementName=root}" />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:vmLearnWrite}">
<local:viewLearnWrite />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:vmLearnListen}">
<local:viewLearnListen />
</DataTemplate>
</Window.Resources>

到:
<Window.Resources>
<DataTemplate DataType="{x:Type vm:vmLearnSpeak}">
<local:viewLearnSpeak Command="{Binding DataContext.cmdVMNotKnown, RelativeSource={RelativeSource AncestorType=local:wndSession}}" />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:vmLearnWrite}">
<local:viewLearnWrite />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:vmLearnListen}">
<local:viewLearnListen />
</DataTemplate>
</Window.Resources>

关于c# - 将 DataTemplate 中的 WPF RelayCommand 绑定(bind)到 UserControl 内的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60191757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com