Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF发布从ControlTemplate到代码隐藏的绑定_Wpf_Binding - Fatal编程技术网

WPF发布从ControlTemplate到代码隐藏的绑定

WPF发布从ControlTemplate到代码隐藏的绑定,wpf,binding,Wpf,Binding,我在App.xaml文件中定义了以下自定义按钮 <Style x:Key="DispatchListCallButton" TargetType="Button"> <Setter Property="OverridesDefaultStyle" Value="True"/> <Setter Property="Template"> <Setter.Value>

我在App.xaml文件中定义了以下自定义按钮

    <Style x:Key="DispatchListCallButton" TargetType="Button">
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Name="outerBorder" BorderThickness="1" BorderBrush="DimGray" CornerRadius="1" Background="{TemplateBinding Background}">
                        <Border Name="innerBorder" BorderThickness="1" BorderBrush="WhiteSmoke" CornerRadius="1" Background="{TemplateBinding Background}">
                            <Grid Margin="2">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="1*"></RowDefinition>
                                    <RowDefinition Height="2*"></RowDefinition>
                                    <RowDefinition Height="Auto"></RowDefinition>
                                    <RowDefinition Height="1*"></RowDefinition>
                                </Grid.RowDefinitions>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock>2600</TextBlock>
                                    <TextBlock Margin="4,0,0,0">IPRJ</TextBlock>
                                </StackPanel>
                                <TextBlock Grid.Row="1" TextWrapping="Wrap">1234 Main St West Avenue</TextBlock>
                                <Rectangle Grid.Row="2" Height="1" Margin="2,0,2,0" Stroke="DarkGray" />
                                <StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Right">
                                    <TextBlock>1</TextBlock>
                                    <TextBlock Margin="4,0,0,0">*</TextBlock>
                                </StackPanel>
                                <ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Center" Name="content"/>
                            </Grid>
                        </Border>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

如何执行此操作以及ControlTemplate中的绑定表达式是什么样子的?

您可以通过以下两种方法之一执行此操作

第一种方法: 首先,您需要创建从Button继承的控件类型,因为您不能像现在尝试的那样修改按钮或按样式实例化。 然后将Id、代码等定义为控件的从属属性。如果它们是DependencyProperties,那么代码将自动注册以侦听更改

第条:

第二种方法: 您将为按钮定义一个ViewModel,该按钮公开这些属性并实现INotifyPropertyChange。每次设置属性时,触发事件。然后将newButton.DataContext设置为ViewModel的实例并修改视图模型。绑定类似于Text={Binding Address}

第条:

谢谢。首选哪种方法且更容易处理。我曾在其他地方使用过第一种方法,但我不想因此而变得如此复杂。我推荐第二种方法。它更符合WPF的模型-视图-视图-模型设计模式,并且如果您以后决定更改显示类型,它将更加灵活。我看到了很多关于MVVM是什么的描述,但我找不到一个带有实际源代码的简单示例。你有什么例子吗?另外,由于你似乎在这方面有知识,你对我的另一个问题有什么建议吗?
        DispatchListCallButton newButton = new DispatchListCallButton();

        // Set the 5 TextBlock values
        newButton.Id = "4444";
        newButton.Code = "ABCD";
        newButton.Address = "2000 Main";
        newButton.Priority = 5;
        newButton.Symbol = "*";