折叠的Gui元素上的装订错误

时间:2019-02-21 10:09:18

标签: c# wpf mvvm

我在网格中有很多绑定,这些绑定根据树视图树中的selecteditem折叠或可见。因此,根据对象,会弹出正确的视图,而其他视图会折​​叠。

但是在折叠视图中,我收到很多gui元素的绑定错误。之所以这样,是因为我在视图模型中有一个“ myselecteditem”,它适合于打开的视图,而不适合折叠的视图。

我的方法愚蠢吗?我可以在折叠的网格中抑制Gui元素的绑定吗?

代码(懒惰的读者的缩写):

XAML:

<Grid x:Name="G_G_Content" Grid.Column="1">
    <Grid x:Name="G_G_Abrechnung_Control" Visibility="Collapsed">
        [...]
    </Grid>
    <Grid x:Name="G_G_Mitglied_Aktion" Visibility="Collapsed">
        [...]
    </Grid>
    <Grid x:Name="G_G_Mitglied_Aktion_Nachweis" Visibility="Visible">
        [...]
    </Grid>
</Grid>

        <TreeView
            x:Name="G_tv_explorer"
            Grid.Row="1"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"
            ItemsSource="{Binding TreeViewItemSource, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
            SelectedItemChanged="G_tv_explorer_SelectedItemChanged"
            TreeViewItem.Expanded="TreeViewItem_Expanded" />

C#:

private void G_tv_explorer_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
        //set active treeviewitem
        if (G_tv_explorer.SelectedItem != null)
        {
            //set treeview && ViMo
            ViMo.TreeViewSelectedItem = e.NewValue;
            RedrawGui();
            ViMo.MySelectedItem = ((TreeViewItem)e.NewValue).Tag;
        }
}

public void RedrawGui()
{
    //redraw gui, dependant on selectedtreeviewitem
    if (ViMo.TreeViewSelectedItem != null)
    {
        if(ViMo.TreeViewSelectedItem is string)
            MessageBox.Show("Dummy");
        else
        {
//this is a placeholder
            if (((TreeViewItem)ViMo.TreeViewSelectedItem).Header.ToString() == "Verwaltung")
            {
                G_G_Mitglied_Aktion_Nachweis.Visibility = System.Windows.Visibility.Collapsed;
                G_G_Abrechnung_Control.Visibility = System.Windows.Visibility.Visible;
            }
            else if (((TreeViewItem)ViMo.TreeViewSelectedItem).Tag is MVVM.Model.Jahresabschluss)
            {
                G_G_Mitglied_Aktion_Nachweis.Visibility = System.Windows.Visibility.Collapsed;
                G_G_Abrechnung.Visibility = System.Windows.Visibility.Visible;
            }
        }
      }
    }

RedrawGUI仅根据SelectedItem的对象类型设置可见/折叠的网格

0 个答案:

没有答案