Wpf触发元素之间用户控件内的事件

时间:2015-01-29 16:50:32

标签: c# .net wpf

我有一个有两个网格的ListViewItem。当用户单击第一个网格时,它会显示第二个网格。但由于它是一个用户控件,我无法在代码隐藏中找到它。

<UserControl>
    <ListView>
        <ListView.ItemTemplate>
            <DataTemplate DataType="models:Tweet">
                <ListViewItem>
                    <Grid Name="Grid1" OnMouseLeftDown="ShowsGrid2"/>
                    <Grid Name="Grid2"/>
                </ListViewItem>
            </DataTemplate>
        </ListView.ItemTemplate>

        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem" BasedOn="{StaticResource {x:Type ListViewItem}}">
                <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDown" />
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>
</UserControl>

public void PreviewMouseLeftButtonDown(object sender)
{
    var control = (Grid)sender;
    sender.Visibility = Visibility.Visible;
}

但我得到:control = null异常

2 个答案:

答案 0 :(得分:0)

我建议您在代码隐藏中创建一个属性:

public Visibility Grid2Visibility { get; set; }

然后,在XAML中将Grid的可见性绑定到该属性:

<Grid ... Visibility="{Binding Grid2Visibility}" ... />

然后,您可以处理您想要的任何事件并相应地设置Grid2的可见性:

public void HandleSomeEvent(object sender, SomeArgs e)
{
    // I can collapse Grid2
    Grid2Visibility = Visibility.Collapsed;

    // or I can show it
    Grid2Visibility = Visibility.Visible;   
 }  

另一种选择是使用Triggers。这是一个快速而肮脏的示例,其中一个按钮的点击事件触发了第二个按钮的可见性:

<StackPanel>
    <Button Content="Button 1">
        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.Click">
                <BeginStoryboard>
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.Target="{x:Reference Button2}" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Visible}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Button.Triggers>
    </Button>
    <Button Name="Button2" Visibility="Collapsed">Button 2</Button>
</StackPanel>

答案 1 :(得分:0)

以下是如何执行代码的简单示例。

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private Visibility _gird2Visibility;
    public Visibility Grid2Visibility
    {
        get { return _gird2Visibility; }
        set
        {
            _gird2Visibility = value;
            OnPropertyChanged("Grid2Visibility");
        }
    }

    public MainWindow()
    {
        InitializeComponent();
        Grid2Visibility = Visibility.Hidden;
    }

    private void Grid1_OnMouseDown(object sender, MouseButtonEventArgs e)
    {
       Grid2Visibility = Visibility.Visible;
    }

    private void Grid1_OnMouseUp(object sender, MouseButtonEventArgs e)
    {
        Grid2Visibility = Visibility.Hidden;
    }

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

这是XAML

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid Grid.Row="0" Name="Grid1" MouseDown="Grid1_OnMouseDown" MouseUp="Grid1_OnMouseUp" >
        <TextBlock Text="CLICK ANYWHERE WITHIN THE GRID TO SHOW SECOND GRID" />
    </Grid>

    <Grid Grid.Row="1"  Name="Grid2" Visibility="{Binding Grid2Visibility}" >
        <TextBlock Text="I AM VISIBLE" />
    </Grid>
</Grid>

我不知道您的窗口的数据上下文。但是,这就是我在我的例子中设置它的方式

DataContext="{Binding RelativeSource={RelativeSource Self}}"