2016-12-15 104 views
1

我試圖根據用戶選擇的listviewitem更新我的主窗口上的控件,但是當listviewitem選擇被更改時,控件不會更新。WPF - 基於listviewitem選擇更改控件。控件不更新

我用這篇文章作爲參考How to dynamically change a WPF control's template using a checkbox?

編輯:我最初使用的ContentTemplate,但改變了這DataTemplate的基礎上建議,但它仍然沒有更新

的XMAL我的主窗口

<Window.Resources> 
    <DataTemplate x:Key="Default"> 
     <Grid Margin="20,280,0,0" /> 
    </DataTemplate> 
    <DataTemplate x:Key="ERAFileSelect"> 
     <Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="218" Margin="20,280,0,0" VerticalAlignment="Top" Width="257" CornerRadius="15"> 
      <Grid Name="grdFileSelect"> 
       <Label x:Name="lblProcessContent" Content="Drag File or Manually Select File" HorizontalAlignment="Center" VerticalAlignment="Top" Width="257" HorizontalContentAlignment="Center"/> 
       <TextBox x:Name="txtEraFileName" HorizontalAlignment="Left" Height="23" Margin="10,80,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="235"/> 
       <Button x:Name="btnSelectFiles" Content="Manually Select File(s)" HorizontalAlignment="Left" Margin="10,161,0,0" VerticalAlignment="Top" Width="235" Height="45"/> 
      </Grid> 
     </Border> 
    </DataTemplate> 
    <DataTemplate x:Key="FCSFileSelect"> 
     <Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="218" Margin="20,280,0,0" VerticalAlignment="Top" Width="257" CornerRadius="15"> 
      <Grid Name="grdFileSelect"> 
       <Label x:Name="lblProcessContent" Content="Drag File or Manually Select Files" HorizontalAlignment="Center" VerticalAlignment="Top" Width="257" HorizontalContentAlignment="Center"/> 
       <TextBox x:Name="txtFCSFileName_TQ02" HorizontalAlignment="Left" Height="23" Margin="10,40,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="174"/> 
       <Button x:Name="btnSelectFiles_TQ02" Content="Select" HorizontalAlignment="Left" Margin="189,37,0,0" VerticalAlignment="Top" Width="56" Height="28"/> 
       <TextBox x:Name="txtFCSFileName_TQ11" HorizontalAlignment="Left" Height="23" Margin="10,105,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="174"/> 
       <Button x:Name="btnSelectFiles_TQ11" Content="Selec" HorizontalAlignment="Left" Margin="189,100,0,0" VerticalAlignment="Top" Width="56" Height="28"/> 
       <TextBox x:Name="txtFCSFileName_TQ16" HorizontalAlignment="Left" Height="23" Margin="10,170,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="174"/> 
       <Button x:Name="btnSelectFiles_TQ16" Content="Select" HorizontalAlignment="Left" Margin="189,165,0,0" VerticalAlignment="Top" Width="56" Height="28"/> 
      </Grid> 
     </Border> 
    </DataTemplate> 
</Window.Resources> 
<Grid Margin="0,0,2,0"> 
    <GroupBox x:Name="gbxProgress" Header="Progress" HorizontalAlignment="Left" Margin="298,105,0,0" VerticalAlignment="Top" Height="445" Width="462" Foreground="Black"> 
     <ListBox x:Name="lbxProgress" HorizontalAlignment="Left" Height="408" Margin="10,10,0,0" VerticalAlignment="Top" Width="431" Foreground="Black" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Progress.Message}" /> 
    </GroupBox> 
    <Button x:Name="btnStart" Content="Start" HorizontalAlignment="Left" Margin="20,513,0,0" VerticalAlignment="Top" Width="100" Height="37" IsEnabled="{Binding Properties.StartButtonEnabled}" Click="btnStart_Click"/> 
    <Button x:Name="btnCancel" Content="Cancel" HorizontalAlignment="Left" Margin="177,513,0,0" VerticalAlignment="Top" Width="100" Height="37"/> 
    <Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="70" Margin="20,21,0,0" VerticalAlignment="Top" Width="740" CornerRadius="15"> 
     <Grid> 
      <Image HorizontalAlignment ="Left" Margin="10" Height="50" Width="50" VerticalAlignment="Center" Source="/Lib/Icons/User.png" /> 
      <TextBlock Name="txtUser" FontSize="20" Height="30" Width="200" Foreground="Red" HorizontalAlignment="Left" Margin="78,19,0,19"/> 
      <Image HorizontalAlignment ="Left" Margin="443,8,0,10" Height="50" Width="50" VerticalAlignment="Center" Source="Lib/Icons/Watch.png" /> 
      <TextBlock x:Name="txtRunTime" FontSize="20" Height="30" Width="200" Foreground="Red" HorizontalAlignment="Left" Margin="519,19,0,19" Text="{Binding AppRunTime.TimeElapsed}" /> 
     </Grid> 
    </Border> 
    <Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="144" Margin="20,119,0,0" VerticalAlignment="Top" Width="257" CornerRadius="15"> 
     <Grid> 
      <Label x:Name="lblProcessSelection" Content="Process Selection" HorizontalAlignment="Center" VerticalAlignment="Top" Width="257" HorizontalContentAlignment="Center"/> 
      <ListView x:Name="lvProcessSelection" HorizontalAlignment="Left" Height="93" Margin="10,30,0,0" VerticalAlignment="Top" Width="235" BorderThickness="0" SelectionChanged="lvProcessSelection_SelectionChanged"> 
       <ListViewItem Name="itmERA" Content="Expense Reserve Automation"/> 
       <ListViewItem Name="itmFCS" Content="Financial Close Status"/> 
       <ListViewItem Name="itmPEL" Content="Peel"/> 
      </ListView> 
     </Grid> 
    </Border> 
    <ContentControl DataContext="{Binding Properties}" Content="{Binding}"> 
    <ContentControl.Style> 
      <Style TargetType="ContentControl"> 
       <Setter Property="ContentTemplate" Value="{StaticResource ERAFileSelect}"/> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding SelectedProcess}" Value="Expense Reserve Automation"> 
         <Setter Property="ContentTemplate" Value="{StaticResource ERAFileSelect}" /> 
        </DataTrigger> 
        <DataTrigger Binding="{Binding SelectedProcess}" Value="Financial Close Status"> 
         <Setter Property="ContentTemplate" Value="{StaticResource FCSFileSelect}" /> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </ContentControl.Style> 
    </ContentControl> 
</Grid> 

視圖模型代碼是

public class MainWindowModel 
{ 
    public ApplicationRunTime AppRunTime { get; set; } 
    public LogMessage Progress { get; set; } 

    public MainWindowProperties Properties { get; set; } 

    public MainWindowModel() 
    { 
     AppRunTime = new ApplicationRunTime(); 
     Progress = new LogMessage(); 
     Properties = new MainWindowProperties(); 
     Properties.StartButtonEnabled = false; 
    } 
} 

隨着MainWindowProperties類

public class MainWindowProperties 
{ 
    public bool StartButtonEnabled { get; set; } 

    public string SelectedProcess { get; set; } 
} 

我的主窗口構造函數中我已經設置了DataContext的

 mainWindowModel = new MainWindowModel(); 
     this.DataContext = mainWindowModel; 

當從lvProcessSelection選擇改變時,下面的代碼被執行

 if (lvProcessSelection.SelectedItems.Count > 0) 
     { 
      mainWindowModel.Properties.SelectedProcess = ((ListViewItem)lvProcessSelection.SelectedItem).Content.ToString(); 
     } 
     else 
     { 
      mainWindowModel.Properties.SelectedProcess = string.Empty; 
     } 

這將使用「費用預留自動化」或「財務結算狀態」在我的ViewModel中更新SelectedProcess

我知道DataContext的正確設置爲我的視圖模型(但可能不是在ContentControl中),因爲我能夠與新的消息來更新lbxProgress並與應用程序運行時更新txtRunTime

然而,當我改變選擇在lvProcessSelection上沒有任何反應; ERAFileSelect的默認控件仍然存在。

有人能指出我如何解決這個問題的正確方向嗎?

回答

1

有人可以指出我在正確的方向如何解決這個問題嗎?

你MainWindowProperties類應實現INotifyPropertyChanged接口並且每當SelectedProcess屬性設置爲一個新值提高變更通知:

public class MainWindowProperties : INotifyPropertyChanged 
{ 
    public bool StartButtonEnabled { get; set; } 

    private string _selectedProcess; 

    public string SelectedProcess 
    { 
     get { return _selectedProcess; } 
     set { _selectedProcess = value; NotifyPropertyChanged(); } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

請參閱MSDN關於這個共同的接口的詳細信息:https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx

您綁定的每個視圖模型/模型都必須實現此接口,併爲要更新的視圖中的任何目標值引發更改通知。

+0

乾杯。任何想法爲什麼我的btnStart的Enabled屬性綁定到Properties.StartButtonEnabled時它的工作原理是從true更改爲false?真的好奇。 – gheff

+0

我看不到你在代碼中設置了StartButtonEnabled屬性的位置,所以我無法回答。但是,如果您打算在運行時動態設置它,則應該像SelectedProcess屬性一樣實現StartButtonEnabled屬性,並在其setter中引發PropertyChanged事件。 – mm8

1

爲什麼你沒有使用數據模板?數據模板將在這種情況下工作。

+0

將ControlTemplate更新爲DataTemplate,然後在ContentControl中設置Property =「ContentTemplate」而不是Property =「Template」不會產生任何影響。控件仍然不更新 – gheff

+0

您需要設置內容控件的內容屬性。內容=「{綁定}」 – Rudra

+0

這沒有任何區別。 ContentControl的其他綁定權限?對不起,我是WPF的新手。我已更新上述XAML以反映您的建議 – gheff