使用mainWindow.xaml集成不同的xaml页面

时间:2016-07-11 18:28:13

标签: c# wpf xaml

我不确定我是否朝着正确的方向前进。 我想在我的应用程序的不同页面上重用页面的某些部分,所以我在用户控件wpf xaml上开发了这些页面。 您能否帮助或建议一些关于如何将不同的xaml页面集成到mainWindow.xaml以便运行我的应用程序的示例。

1 个答案:

答案 0 :(得分:0)

so I developed those pages on user control wpf xaml

Here is a design philosophy, place all those user controls on the page in a common frame/location, then in the VM create an enum which will define the different states of the page which directly relate to each control.

Bind all the controls visibility to a property on the VM which identifies the current state (from the aforementioned state enum) and then using a converter hide or show the control.


Here is an example, place the enum on the VM class.

  public enum Operations
  {
     Authentication    = -1,
     DisplayResults    = 0,
     EditData,
     ...
  }

Create a property which will define the current state on the VM

public Operations CurrentState
{
    get { return _CurrentState; }
    set { _CurrentState = value; PropertyChanged(); }
}

Then on the control bind to that state, but also pass in text to identify the control

<controls:Authentication 
             Visibility="{Binding CurrentState, 
                                  ConverterParameter=Authentication, 
                                  Converter={StaticResource StateToVisiblity}}"/>

Then in our converter reference the enums and based on the current state determine whether the control should be shown or not:

public class OperationStateToVisiblity : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (value != null)     && 
               (parameter != null) &&
                value.ToString().Equals(parameter.ToString())
            ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

That way each control only is shown when the state changes and that control is defined to a specific enum.