WPF用户控件数据绑定不工作
我正在创建一个简单的用户控件,将弹出窗口与文本视图相结合,这并不疯狂。当我一开始在窗口中设置它以将其全部样式化时,它工作得非常好,但当我将它移动到用户控件中以实际完成它时,它将不再正常工作 我将一个最小值和最大值传递给控件,然后它会自动创建一个数字列表,在该范围内进行选择。在用户控件中,数字列表没有正确绑定,谁知道为什么。也许有人可以看看我的代码 我读过很多关于这方面的问题,但我真的不知道发生了什么。我的输出窗口中没有任何错误,因此没有任何线索。不管怎样,这是代码- UserControl.xamlWPF用户控件数据绑定不工作,wpf,vb.net,data-binding,user-controls,wpf-controls,Wpf,Vb.net,Data Binding,User Controls,Wpf Controls,我正在创建一个简单的用户控件,将弹出窗口与文本视图相结合,这并不疯狂。当我一开始在窗口中设置它以将其全部样式化时,它工作得非常好,但当我将它移动到用户控件中以实际完成它时,它将不再正常工作 我将一个最小值和最大值传递给控件,然后它会自动创建一个数字列表,在该范围内进行选择。在用户控件中,数字列表没有正确绑定,谁知道为什么。也许有人可以看看我的代码 我读过很多关于这方面的问题,但我真的不知道发生了什么。我的输出窗口中没有任何错误,因此没有任何线索。不管怎样,这是代码- UserControl.xa
<UserControl x:Class="UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Me"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<StackPanel>
<TextBox Name="myTextbox"
Height="30"
Margin="0"
FontSize="14"
IsReadOnly="True"
Padding="5,2"
Text="{Binding Value}" />
<Popup x:Name="myPopup"
PlacementTarget="{Binding ElementName=myTextbox}"
StaysOpen="True">
<Popup.Style>
<Style TargetType="{x:Type Popup}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=myTextbox, Path=IsFocused}" Value="True">
<Setter Property="IsOpen" Value="True" />
</DataTrigger>
</Style.Triggers>
</Style>
</Popup.Style>
<StackPanel>
<ListView Name="myListView"
Height="100"
MaxHeight="300"
ItemsSource="{Binding List,
UpdateSourceTrigger=PropertyChanged}"
SelectionChanged="ListView_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<Label Width="100"
Height="30"
Margin="0"
Content="{Binding}"
FontFamily="Segoe UI"
FontSize="14"
Padding="5,2" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Width="200" Height="30" />
</StackPanel>
</Popup>
</StackPanel>
仅供参考,当我测试这一点时,我自己在window和usercontrol设置中设置了最小值和最大值。我认为您犯了与我第一次开始学习WPF时相同的错误。我不能保证这就是你的问题的原因,但因为这是我能看到的唯一原因,我会解决它。不幸的是,有太多糟糕的教程和快速示例显示了
UserControl.DataContext
与自身的连接:
DataContext="{Binding RelativeSource={RelativeSource Self}}"
或:
现在,如果您不想从外部将绑定到用户控件
,那么这是完全可以接受的,因为这是一种与代码隐藏中定义的属性连接的快速简便的方法。但是,如果要从控件外部将
绑定到属性,则会发现问题。在这些情况下(如果不是在所有情况下),您应该对代码隐藏属性使用相对资源绑定
到绑定
:
<TextBox Name="myTextbox" Height="30" Margin="0" FontSize="14" IsReadOnly="True"
Padding="5,2" Text="{Binding Value, RelativeSource={RelativeSource AncestorType={
x:Type UserControl1}}}" />
在此绑定中,UserControl1
是声明属性的UserControl
的名称。这应该在所有内部绑定上完成。这样,DataContext
就不会设置为UserControl
,但是绑定仍然可以找到属性
您可以从MSDN上的页面了解更多有关相对资源的信息。由于我无法向Sheridan提供评论,所以我必须提供一个新的答案,对此表示抱歉
虽然我喜欢这个解决方案
DataContext="{Binding RelativeSource={RelativeSource Self}}"
它很快就失败了(谢里登已经指出)
您可以做的只是设置用户控件内容的DataContext
<UserControl x:Class="Example.View.Controls.MyUserControl"
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:controls="clr-namespace:Example.View.Controls"
mc:Ignorable="d">
<Grid DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:MyUserControl}}}">
</Grid>
通过这种方式,以下所有绑定都具有较少的样板代码,因为您可以直接从后面的代码绑定到DP,如:
<Label Content="{Binding MyLabel}"/>
对于Windows应用程序(Windows 8和Windows 10 UWP)方法是为控件指定一个名称,并使用路径和元素名称在XAML文件中引用它:
<UserControl
x:Class="MyControl"
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"
x:Name="Control"
mc:Ignorable="d" >
<Grid Height="240" VerticalAlignment="Top">
<Rectangle Fill="{Binding ElementName=Control, Path=Background}" />
</Grid>
</UserControl>
``谢谢。同样的问题也存在,这也适用于c#
<Label Content="{Binding MyLabel}"/>
<UserControl
x:Class="MyControl"
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"
x:Name="Control"
mc:Ignorable="d" >
<Grid Height="240" VerticalAlignment="Top">
<Rectangle Fill="{Binding ElementName=Control, Path=Background}" />
</Grid>
</UserControl>