亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 MVVM 模式在 WPF 中動態綁定 UserControl

如何使用 MVVM 模式在 WPF 中動態綁定 UserControl

C#
慕標琳琳 2021-11-14 15:02:12
目前我正在使用 WPF MVVM 應用程序,我想在其中使用 ItemsControl 在 MainWindow.xaml 中顯示 UserControl。根據要求,我必須通過從 ViewModel 管理一些條件來切換或綁定不同的用戶控件。正如我在下面的代碼中提到的,應該綁定 UserControl。但目前的問題是它的靜態和完美的工作。但我無法根據條件更改它。那么有什么可以在運行時從 ViewModel 更改 UserControlColumn1XL 的嗎?提前致謝。<StackPanel Grid.Column="0" Background="Black" VerticalAlignment="Top" >            <ItemsControl ItemsSource="{Binding NormalCollection}" Name="ListNormal" Margin="4,0" >                <ItemsControl.ItemsPanel>                    <ItemsPanelTemplate>                        <WrapPanel HorizontalAlignment="Left" />                    </ItemsPanelTemplate>                </ItemsControl.ItemsPanel>                <ItemsControl.ItemTemplate>                    <DataTemplate>                        <!--<This below line should be dynamically change through MVVM code>-->                        <controls:UserControlColumn1XL HorizontalAlignment="Left" Margin="2" />                    </DataTemplate>                </ItemsControl.ItemTemplate>            </ItemsControl>    </StackPanel>
查看完整描述

2 回答

?
小唯快跑啊

TA貢獻1863條經驗 獲得超2個贊

這里有一個使用的例子DataTemplateSelector......


這里有一個簡單的ViewModel:


public class YourCustomObject : INotifyPropertyChanged

{

    public string YourProperty { get; set; }  // Remember to rise propertychanged


    public event PropertyChangedEventHandler PropertyChanged;

}



public class YourViewModel 

{

    public ObservableCollection<YourCustomObject> Objects { get; set; }

}

定義要使用的不同模板:


<ItemsControl ItemsSource="{Binding Objects}" Name="ListNormal" Margin="4,0">

    <ItemsControl.Resources>

        <DataTemplate x:Key="firstDataTemplate">

            <UserControl1 />

        </DataTemplate>


        <DataTemplate x:Key="otherDataTemplate">

            <UserControl2 />

        </DataTemplate>

    </ItemsControl.Resources>

創建一個繼承自的類DataTemplateSelector:


 public class MySelector : DataTemplateSelector

{

    public override DataTemplate SelectTemplate(object item, DependencyObject container)

    {

        FrameworkElement element = container as FrameworkElement;


        if (element != null && item != null && item is YourCustomObject)

        {

            YourCustomObject taskitem = item as YourCustomObject;


            // HERE i'm putting the custom logic for choosing the correct template

            if (taskitem.YourProperty == "Value 1")

                return

                    element.FindResource("firstDataTemplate") as DataTemplate;

            else

                return

                    element.FindResource("otherDataTemplate") as DataTemplate;

        }


        return null;

    }

}

將選擇器的一個實例添加到您的主視圖中:


<Window.Resources>

    <local:MySelector x:Key="Selector"/>

</Window.Resource>

將的ItemTemplateSelector屬性設置ItemsControl為 Selector:


<ItemsControl ItemsSource="{Binding Objects}" Name="ListNormal" Margin="4,0" ItemTemplateSelector="{StaticResource Selector}">


查看完整回答
反對 回復 2021-11-14
?
MMTTMM

TA貢獻1869條經驗 獲得超4個贊

通常這樣做的方法是使用 DataTemplates。從ObservableCollection<object> Objects您的 ViewModel 中的an 開始...它不一定是<object>,如果您愿意,它可以是一些常見的基類。然后在您的 XAML 中為每種類型添加 DataTemplates。我把它們放在ItemsControl.Resources這里,但你可以把它們放在UserControl.Resources你覺得合適的地方:


<ItemsControl>

    <ItemsControl.Resources>


        <DataTemplate DataType="{x:Type viewmodels:YourViewModel1}">

            <views:UserControl1 />

        </DataTemplate>


        <DataTemplate DataType="{x:Type viewmodels:YourViewModel2}">

            <views:UserControl2 />

        </DataTemplate>


        ... etc ...

還要刪除<ItemsControl.ItemTemplate>標簽,因為如果不這樣做,它只會覆蓋 DataTemplates。


這就是你需要做的!每次有一個類型的對象時YourViewModel1,Objects它都會與 UserControl1 一起顯示,并且對于您指定的所有其他模板也是如此。


查看完整回答
反對 回復 2021-11-14
  • 2 回答
  • 0 關注
  • 839 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號