大家好,请问资源字典里定义的控件,子控件事件怎么加?在线等,24点前在线

zhangyonghui2117 2015-01-29 10:47:39
第一:如题:
给个示范图片:
图片与它下面的标签是一个资源模板,类型为DataTemplate,现在我能给它添加一个点击事件,但是这个事件是针对整个DataTemplate的,我想只点击上面的按钮才响应点击事件,上图是添加到scrollviewer与wrappanel,实现翻页。
第二:如果按钮事件添加不了,可不可以在在wrap里添加Window控件,我把图片与文字还有按钮做在一个window里,但好像wrappanel添加不了window,只能添加Button类的控件,在线等,谢谢
...全文
190 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
exception92 2015-02-03
  • 打赏
  • 举报
回复
引用 7 楼 u011785544 的回复:
可以搞一个帮助类,让它继承资源字典 也就是ResourcesDictionary 加上partial 在这里写一个事件处理方法 在你的资源字典里 引用上这个类 如:x:Class="Class1" 这样就好了
搞个例子看看,,
exception92 2015-02-03
  • 打赏
  • 举报
回复
引用 7 楼 u011785544 的回复:
可以搞一个帮助类,让它继承资源字典 也就是ResourcesDictionary 加上partial 在这里写一个事件处理方法 在你的资源字典里 引用上这个类 如:x:Class="Class1" 这样就好了
搞个例子看看,,
exception92 2015-01-30
  • 打赏
  • 举报
回复
引用 4 楼 zhangyonghui2117 的回复:
[quote=引用 2 楼 duanzi_peng 的回复:] 把图片与文字还有按钮 做成一个UserControl 控件,在UserControl 控件处理你的button事件 就行了。
你好,你的方法肯定可行的,为了省点时间,麻烦帮我看下我这样做怎么改,谢谢,我现在是这样做的,把他们做成一个DataTemplate数据模板放在资源字典中,模板内容如下:

<DataTemplate x:Key="MeetingInfoTemplate">
        <Border Width="407" Height="351">
            <Border.Background>
                <ImageBrush ImageSource="Images/MeetingInfo.png" />
            </Border.Background>
            <Grid ShowGridLines="False">
                <Grid.RowDefinitions>
                    <RowDefinition Height="52"></RowDefinition>
                    <RowDefinition Height="46"></RowDefinition>
                    <RowDefinition Height="180"></RowDefinition>
                    <RowDefinition Height="76"></RowDefinition>
                </Grid.RowDefinitions>
                <Label FontFamily="微软雅黑" FontSize="16" Grid.Row="0" Content=" 标题"/>
                <Label FontFamily="微软雅黑" FontSize="16" Grid.Row="1" Content=" 内容:"/>
                <Label Content="房间" FontFamily="微软雅黑" FontSize="16" Grid.Row="2"/>
                <local:ImageButton Width="105" Height="35" VerticalAlignment="Center" HorizontalAlignment="Center" x:Name="btnExit2" ImagePath="/Images/join_btn.png" Template="{StaticResource ImageButtonTemplate}"  Grid.Row="3" />
            </Grid>
        </Border>
    </DataTemplate>
cs文件如下:

       UserControl tempctl = new UserControl();
                tempctl.Margin = new Thickness(15, 15, 15, 15);
                tempctl.Content = temp;
                tempctl.ContentTemplate = (System.Windows.DataTemplate)FindResource("MeetingInfoTemplate");// 这里加载模板
                tempctl.MouseLeftButtonUp += new MouseButtonEventHandler(scencectl_MouseLeftButtonUp);
                tempctl.MouseDoubleClick += new MouseButtonEventHandler(scencectl_MouseDoubleClick);
                m_scencectllist.Add(tempctl);
            }
但现在scencectl_MouseDoubleClick事件是响应整个template的,即点即文字区域也响应,有什么方法只点击按钮才响应,[/quote] 在事件中 判断e.OriginalSource
「已注销」 2015-01-30
  • 打赏
  • 举报
回复
引用 3 楼 duanzi_peng 的回复:
另,wrapPanel 是添加不了 Window窗口的。
ListBox可以添加吗,实现的效果应该一样把?
「已注销」 2015-01-30
  • 打赏
  • 举报
回复
引用 2 楼 duanzi_peng 的回复:
把图片与文字还有按钮 做成一个UserControl 控件,在UserControl 控件处理你的button事件 就行了。
你好,你的方法肯定可行的,为了省点时间,麻烦帮我看下我这样做怎么改,谢谢,我现在是这样做的,把他们做成一个DataTemplate数据模板放在资源字典中,模板内容如下:

<DataTemplate x:Key="MeetingInfoTemplate">
        <Border Width="407" Height="351">
            <Border.Background>
                <ImageBrush ImageSource="Images/MeetingInfo.png" />
            </Border.Background>
            <Grid ShowGridLines="False">
                <Grid.RowDefinitions>
                    <RowDefinition Height="52"></RowDefinition>
                    <RowDefinition Height="46"></RowDefinition>
                    <RowDefinition Height="180"></RowDefinition>
                    <RowDefinition Height="76"></RowDefinition>
                </Grid.RowDefinitions>
                <Label FontFamily="微软雅黑" FontSize="16" Grid.Row="0" Content=" 标题"/>
                <Label FontFamily="微软雅黑" FontSize="16" Grid.Row="1" Content=" 内容:"/>
                <Label Content="房间" FontFamily="微软雅黑" FontSize="16" Grid.Row="2"/>
                <local:ImageButton Width="105" Height="35" VerticalAlignment="Center" HorizontalAlignment="Center" x:Name="btnExit2" ImagePath="/Images/join_btn.png" Template="{StaticResource ImageButtonTemplate}"  Grid.Row="3" />
            </Grid>
        </Border>
    </DataTemplate>
cs文件如下:

       UserControl tempctl = new UserControl();
                tempctl.Margin = new Thickness(15, 15, 15, 15);
                tempctl.Content = temp;
                tempctl.ContentTemplate = (System.Windows.DataTemplate)FindResource("MeetingInfoTemplate");// 这里加载模板
                tempctl.MouseLeftButtonUp += new MouseButtonEventHandler(scencectl_MouseLeftButtonUp);
                tempctl.MouseDoubleClick += new MouseButtonEventHandler(scencectl_MouseDoubleClick);
                m_scencectllist.Add(tempctl);
            }
但现在scencectl_MouseDoubleClick事件是响应整个template的,即点即文字区域也响应,有什么方法只点击按钮才响应,
exception92 2015-01-30
  • 打赏
  • 举报
回复
另,wrapPanel 是添加不了 Window窗口的。
exception92 2015-01-30
  • 打赏
  • 举报
回复
把图片与文字还有按钮 做成一个UserControl 控件,在UserControl 控件处理你的button事件 就行了。
「已注销」 2015-01-30
  • 打赏
  • 举报
回复
大家好,望各位能给个方案,谢谢
Veary 2015-01-30
  • 打赏
  • 举报
回复
可以搞一个帮助类,让它继承资源字典 也就是ResourcesDictionary 加上partial 在这里写一个事件处理方法 在你的资源字典里 引用上这个类 如:x:Class="Class1" 这样就好了

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧