c# - 将数据绑定(bind)到 WPF 用户控件

标签 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"
             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>

这是后面的代码
public partial class GridRow : UserControl
    {
        public bool IsChecked { get; set; }
        public int EventId { get; set; }
        public string MethodName { get; set; }
        public string Level { get; set; }
        public string Opcode { get; set; }
        public string Task { get;set; }
        public string Keyword { get; set; }

        public GridRow()
        {
            InitializeComponent();


        }

        private void OnClickEdit(object sender, RoutedEventArgs e)
        {

        }

        private void OnClickDelete(object sender, RoutedEventArgs e)
        {

        }
    }

现在你能告诉我在双向模式下将文件背后的代码属性绑定(bind)到 UI 时错过了什么重要的事情。

虽然这不是 MVVM 方式..

最佳答案

添加 x:Name到您的控件并使用 ElementName 绑定(bind)到属性:

<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>

如果你想支持更新值,你应该使用 DependencyProperties而不是正常的属性:
public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool), typeof(GridRow));

public bool IsChecked
{
    get { return (bool)GetValue(IsCheckedProperty); }
    set { GetValue(IsCheckedProperty, value); }
}

关于c# - 将数据绑定(bind)到 WPF 用户控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26800717/

相关文章:

c# - 如何在我的 DataGridViewImageColumn 中添加图像?

c# - 我可以在 WinRT 中更改主磁贴的背景颜色吗?

c# - 错误! (使用来自 C# 的 Excel 命名范围)

c# - 从 C# 程序中反序列化 JSON 时,我是否需要使用 JavaScriptSerializer 以外的任何东西?

c# - WPF RibbonWindow 中意外的标题栏行为

c# - Elasticsearch.NET和NEST-搜索总是返回0个结果

c# - 从 GridView C# 中的 DataRow 获取原始对象

c# - 我必须在 Windows 服务中实现 Stop 方法吗?

.net - WPF中的TabIndex与KeyboardNavigation.TabIndex

wpf - 如何更改要使用的数据库的路径?