WPF报错“Binding仅能设置于DependencyObject的DependencyProperty”求助
WPF绑定错误排查:“binding can only be set in dependencyproperty of dependencyobject”
我写了一段WPF XAML代码,运行时抛出错误:“binding can only be set in dependencyproperty of dependencyobject”,搞不懂错误原因,也不明白为什么要加依赖项。
出错的XAML代码
<Window x:Class="Kiosk.DeleteMenu.DeleteMenuWindow" 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:Kiosk.DeleteMenu" mc:Ignorable="d" Title="DeleteMenuWindow" Height="800" Width="800"> <Grid> <ListBox ItemsSource="{Binding Menus}" Width="800"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <WrapPanel Width="800"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ListBox.ItemTemplate> <DataTemplate> <Border BorderThickness="1" BorderBrush="Black" Width="250" Background="Transparent"> <StackPanel Orientation="Vertical" Width="250"> <StackPanel.InputBindings> <MouseBinding MouseAction="LeftClick" Command="{Binding StackPanelClickCommands}" CommandParameter="{Binding ElementName=lb_MenuText, Path=Content}"></MouseBinding> </StackPanel.InputBindings> <Image Width ="150" Height="150" Stretch="Uniform" StretchDirection="Both" Source="{Binding Path = MenuImagetoBS}"> </Image> <Border BorderThickness="1" BorderBrush="Silver"> <Label x:Name ="lb_MenuText" Content="{Binding Path = MenuText}"></Label> </Border> <Label Content="{Binding Path = MenuKind}"></Label> </StackPanel> </Border> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </Window>
尝试的修改
我把MouseBinding从StackPanel移到Image元素里,但错误依然存在:
<Image.InputBindings> <MouseBinding MouseAction="LeftClick" Command="{Binding StackPanelClickCommands}" CommandParameter="{Binding ElementName=lb_MenuText, Path=Content}"></MouseBinding> </Image.InputBindings>
可正常运行的对比代码
但我另一个项目里的类似代码能正常运行:
<Image Margin="757,222,312,373"> <Image.Resources> <Style TargetType="{x:Type Image}"> <!-- Default image --> <Setter Property="Source" Value="Resources/DefaultPoint.png"/> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <!-- Hover image --> <Setter Property="Source" Value="Resources/MapPoint.png"/> </Trigger> </Style.Triggers> </Style> </Image.Resources> <Image.ToolTip> <ToolTip x:Name="USToolTip2" Width="250"> <StackPanel Background="White" Width="250"> <TextBlock FontWeight="Bold" Width="{Binding ActualWidth, ElementName=USToolTip}"><Run Foreground="Red" Text=" : " /><Run Text=" "/><Run Foreground="Black" Text="{Binding Covid.Country}"/></TextBlock> <TextBlock FontWeight="Bold"><Run Foreground="Red" Text=": "/><Run Text=" "/><Run Foreground="Black" Text="{Binding Covid.Confirmedcase}"/></TextBlock> <TextBlock FontWeight="Bold"><Run Foreground="Red" Text=": "/><Run Text=" "/><Run Foreground="Black" Text="{Binding Covid.Dead}"/></TextBlock> <TextBlock FontWeight="Bold"><Run Foreground="Red" Text=": "/><Run Text=" "/><Run Foreground="Black" Text="{Binding Covid.Cured}"/></TextBlock> <TextBlock FontWeight="Bold"><Run Foreground="Red" Text=": "/><Run Text=" "/><Run Foreground="Black" Text="{Binding Covid.Fatality}"/></TextBlock> <TextBlock FontWeight="Bold"><Run Foreground="Red" Text=": "/><Run Text=" "/><Run Foreground="Black" Text="{Binding Covid.CuredPercent}"/></TextBlock> <TextBlock FontWeight="Bold"><Run Foreground="Red" Text=": "/><Run Text=" "/><Run Foreground="Black" Text="{Binding Covid.Incidence}"/></TextBlock> </StackPanel> </ToolTip> </Image.ToolTip> <Image.InputBindings> <MouseBinding MouseAction="LeftClick" Command="{Binding MapPointCommand}" CommandParameter="🇮🇳"/> </Image.InputBindings>
错误原因与解决方案
核心错误原因
问题出在**MouseBinding的CommandParameter属性**:
MouseBinding不是DependencyObject,它的CommandParameter是普通CLR属性,不支持数据绑定。直接给它绑定ElementName,就会触发“只能在DependencyObject的DependencyProperty上设置绑定”的错误。- 另一个项目的代码是给
CommandParameter赋了常量值🇮🇳,没有用绑定,所以不会触发这个限制。
两种修复方案
方案1:直接绑定数据项的属性
既然lb_MenuText的Content绑定的是MenuText,直接绑定这个属性即可,不用通过元素查找:
<MouseBinding MouseAction="LeftClick" Command="{Binding StackPanelClickCommands}" CommandParameter="{Binding MenuText}"/>
方案2:用DependencyProperty中转绑定
如果必须通过元素获取值,可以先把目标值存到父元素的Tag属性(Tag是DependencyProperty,支持绑定),再绑定Tag:
<StackPanel Orientation="Vertical" Width="250" Tag="{Binding ElementName=lb_MenuText, Path=Content}"> <StackPanel.InputBindings> <MouseBinding MouseAction="LeftClick" Command="{Binding StackPanelClickCommands}" CommandParameter="{Binding Tag, RelativeSource={RelativeSource AncestorType=StackPanel}}"/> </StackPanel.InputBindings> <!-- 其余元素保持不变 --> </StackPanel>
内容的提问来源于stack exchange,提问作者junsuchoi
相关产品推荐
相关产品推荐

