C# 将数据绑定到WPF UserControl
我已经创建了以下GridRow作为UserControlC# 将数据绑定到WPF UserControl,c#,.net,wpf,xaml,mvvm,C#,.net,Wpf,Xaml,Mvvm,我已经创建了以下GridRow作为UserControl <UserControl x:Class="Project.Telematics_Plugin.GridRow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x
<UserControl x:Class="Project.Telematics_Plugin.GridRow"
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"
mc:Ignorable="d" BorderBrush="LightBlue"
MaxHeight="30" MinWidth="900">
<Grid>
<StackPanel Orientation="Horizontal">
<CheckBox VerticalAlignment="Center" IsChecked="{Binding IsChecked}" />
<TextBox Width="60" Text="{Binding EventId}"/>
<TextBox Width="300" Text="{Binding MethodName}" />
<ComboBox Width="200" ItemsSource="{Binding }" />
<ComboBox Width="200"/>
<ComboBox Width="200"/>
<Button Click="OnClickEdit">
<Image Source="Images/edit.png"/>
</Button>
<Button Click="OnClickDelete">
<Image Source="Images/delete.png"/>
</Button>
</StackPanel>
</Grid>
</UserControl>
现在,您能告诉我,在双向模式中将代码隐藏文件的属性绑定到UI时,我遗漏了什么重要的事情吗
尽管这不是MVVM的方式。当您使用usercontrol的数据上下文中的所有属性都已检查、EventId、MethodName等时,您可以从usercontrol中删除这些属性,并进行所有操作 但是如果你想创建一个“真正的”用户控件,那么你应该使用DependencyProperties并在你的用户控件中用正确的表达式绑定它们
顺便说一句,当您在WPF中使用绑定时,右DataContext和右BindingExpression都是相关的,当您使用usercontrol的DataContext的所有属性都被选中时,EventId、MethodName,。。。,然后您可以从usercontrol和所有works中删除属性 但是如果你想创建一个“真正的”用户控件,那么你应该使用DependencyProperties并在你的用户控件中用正确的表达式绑定它们
顺便说一句,当您在WPF中使用绑定时,右DataContext和右BindingExpression都是相关的,当您使用usercontrol的DataContext的所有属性都被选中时,EventId、MethodName,。。。,然后您可以从usercontrol和所有works中删除属性 但是如果你想创建一个“真正的”用户控件,那么你应该使用DependencyProperties并在你的用户控件中用正确的表达式绑定它们
顺便说一句,当您在WPF中使用绑定时,右DataContext和右BindingExpression都是相关的,当您使用usercontrol的DataContext的所有属性都被选中时,EventId、MethodName,。。。,然后您可以从usercontrol和所有works中删除属性 但是如果你想创建一个“真正的”用户控件,那么你应该使用DependencyProperties并在你的用户控件中用正确的表达式绑定它们
顺便说一句,当您在WPF中使用绑定时,右DataContext和右BindingExpression将
x:Name
添加到控件中,并使用ElementName
绑定到属性:
<UserControl x:Name="MyGridRow">
<Grid>
<StackPanel Orientation="Horizontal">
<CheckBox VerticalAlignment="Center" IsChecked="{Binding IsChecked, ElementName=MyGridRow}" />
<TextBox Width="60" Text="{Binding EventId, ElementName=MyGridRow}"/>
<TextBox Width="300" Text="{Binding MethodName, ElementName=MyGridRow}" />
<ComboBox Width="200" ItemsSource="{Binding Path=., ElementName=MyGridRow}" />
<ComboBox Width="200"/>
<ComboBox Width="200"/>
<Button Click="OnClickEdit">
<Image Source="Images/edit.png"/>
</Button>
<Button Click="OnClickDelete">
<Image Source="Images/delete.png"/>
</Button>
</StackPanel>
</Grid>
</UserControl>
将
x:Name
添加到控件中,并使用ElementName
绑定到属性:
<UserControl x:Name="MyGridRow">
<Grid>
<StackPanel Orientation="Horizontal">
<CheckBox VerticalAlignment="Center" IsChecked="{Binding IsChecked, ElementName=MyGridRow}" />
<TextBox Width="60" Text="{Binding EventId, ElementName=MyGridRow}"/>
<TextBox Width="300" Text="{Binding MethodName, ElementName=MyGridRow}" />
<ComboBox Width="200" ItemsSource="{Binding Path=., ElementName=MyGridRow}" />
<ComboBox Width="200"/>
<ComboBox Width="200"/>
<Button Click="OnClickEdit">
<Image Source="Images/edit.png"/>
</Button>
<Button Click="OnClickDelete">
<Image Source="Images/delete.png"/>
</Button>
</StackPanel>
</Grid>
</UserControl>
将
x:Name
添加到控件中,并使用ElementName
绑定到属性:
<UserControl x:Name="MyGridRow">
<Grid>
<StackPanel Orientation="Horizontal">
<CheckBox VerticalAlignment="Center" IsChecked="{Binding IsChecked, ElementName=MyGridRow}" />
<TextBox Width="60" Text="{Binding EventId, ElementName=MyGridRow}"/>
<TextBox Width="300" Text="{Binding MethodName, ElementName=MyGridRow}" />
<ComboBox Width="200" ItemsSource="{Binding Path=., ElementName=MyGridRow}" />
<ComboBox Width="200"/>
<ComboBox Width="200"/>
<Button Click="OnClickEdit">
<Image Source="Images/edit.png"/>
</Button>
<Button Click="OnClickDelete">
<Image Source="Images/delete.png"/>
</Button>
</StackPanel>
</Grid>
</UserControl>
将
x:Name
添加到控件中,并使用ElementName
绑定到属性:
<UserControl x:Name="MyGridRow">
<Grid>
<StackPanel Orientation="Horizontal">
<CheckBox VerticalAlignment="Center" IsChecked="{Binding IsChecked, ElementName=MyGridRow}" />
<TextBox Width="60" Text="{Binding EventId, ElementName=MyGridRow}"/>
<TextBox Width="300" Text="{Binding MethodName, ElementName=MyGridRow}" />
<ComboBox Width="200" ItemsSource="{Binding Path=., ElementName=MyGridRow}" />
<ComboBox Width="200"/>
<ComboBox Width="200"/>
<Button Click="OnClickEdit">
<Image Source="Images/edit.png"/>
</Button>
<Button Click="OnClickDelete">
<Image Source="Images/delete.png"/>
</Button>
</StackPanel>
</Grid>
</UserControl>
添加了一个示例
DependencyProperty
。值仍然没有反映在UI上,我认为上面的代码中缺少了一些小东西。这很奇怪,您能否共享控件的实例化,在其中设置属性?哦,我忘了提到dependencProperty
应该是只读的
,但是如果您正在使用它们,这不应该引起问题。顺便问一下,绑定具有多个值的ComboBox类型控件的最佳方法是什么..我不太清楚您的意思,这听起来像是一个新问题。尝试搜索SO,如果找不到,请创建一个新问题。添加了一个示例DependencyProperty
。值仍然没有反映在UI上,我认为上面的代码中缺少一些小东西。这很奇怪,您能否共享控件的实例化,在其中设置属性?哦,我忘了提到dependencProperty
应该是只读的
,但是如果您正在使用它们,这不应该引起问题。顺便问一下,绑定具有多个值的ComboBox类型控件的最佳方法是什么..我不太清楚您的意思,这听起来像是一个新问题。尝试搜索SO,如果找不到,请创建一个新问题。添加了一个示例DependencyProperty
。值仍然没有反映在UI上,我认为上面的代码中缺少一些小东西。这很奇怪,您能否共享控件的实例化,在其中设置属性?哦,我忘了提到dependencProperty
应该是只读的
,但是如果您正在使用它们,这不应该引起问题。顺便问一下,绑定具有多个值的ComboBox类型控件的最佳方法是什么..我不太清楚您的意思,这听起来像是一个新问题。尝试搜索SO,如果找不到,请创建一个新问题。添加了一个示例DependencyProperty
。值仍然没有反映在UI上,我认为上面的代码中缺少一些小东西。这很奇怪,您能否共享控件的实例化,在其中设置属性?哦,我忘了提到dependencProperty
应该是只读的
,但是如果您正在使用它们,这不应该引起问题。顺便问一下,绑定具有多个值的ComboBox类型控件的最佳方法是什么..我不太清楚您的意思,这听起来像是一个新问题。尝试搜索SO,如果找不到,请创建一个新问题。