【问题标题】:Button Click event from Resource Directory资源目录中的按钮单击事件
【发布时间】:2012-09-19 02:47:51
【问题描述】:

我构建了一个带有两个按钮的控件模板,我在 MainWindow 类中使用:

控制模板:

<ControlTemplate x:Key="LeftPanelTemplate">
    <Grid  Grid.Column="0"  Grid.Row="0" Margin="10,15,5,5" >
        <Border BorderThickness="7" CornerRadius="4">
            <Border.BorderBrush>
                <SolidColorBrush Color="#73B2F5" Opacity="0.5"/>
            </Border.BorderBrush>
            <Grid>
                <Grid.Background>
                    <SolidColorBrush Color="#73B2F5" Opacity="0.5"/>
                </Grid.Background>
                <Grid.RowDefinitions>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="1*"/>
                </Grid.RowDefinitions>
                <Button Name="CustomerButton" Grid.Row="1" Grid.Column="0" Width="40" Height="40" Content="Customer" Click="CustTabButton_Click" ></Button>
                <Button Name="BossButton" Grid.Row="1" Width="40" Height="40" Content="Boss" Margin="23,206,23,114" Click="BossTabButton_Click"></Button>
            </Grid>
        </Border>
    </Grid>
</ControlTemplate>

我在 MainWindow XAML 中引用它:

<ContentControl Template="{StaticResource LeftPanelTemplate}"/>

我收到一个错误,即无法找到控件模板内的两个按钮的点击事件 - 因为我将它移到名为 MainWindowResources.xaml 的 ResourceDirectory 文件中:

'MyWPFApp.MainWindowResource' 不包含对 'CustTabButton_Click' 和无扩展方法 'CustTabButton_Click' 接受“MyWPFApp.MainWindowResource”类型的第一个参数可以 被发现(您是否缺少 using 指令或程序集引用?)

我找到了这个链接:Is it possible to set code behind a resource dictionary in WPF for event handling?

是否有更短的方法可以让后面的 MainWindow 代码中的点击事件从资源目录中执行?因为将整个代码从 MainWindow 移动到 REsourceDirectory.xaml.code 可能会花费大量时间并且会公开大量变量。

【问题讨论】:

    标签: c# wpf xaml resourcedictionary


    【解决方案1】:

    您可以使用命令而不是按钮单击并将命令从主窗口绑定到用户控件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多