2016-12-15 7 views
1

ユーザーが選択したlistviewitemに基づいてメインウィンドウにあるコントロールを更新しようとしていますが、listviewitemの選択が変更されたときにコントロールが更新されません。WPF - listviewitemの選択に基づいてコントロールを変更します。コントロールが更新されない

私が参照How to dynamically change a WPF control's template using a checkbox?

EDITとしてこの記事を使用:I最初に使用ContentTemplateが、提案に基づいたDataTemplateにこれを変更し、しかし、それはまだ私のメインウィンドウが

あるため

のXmaIを更新していません

<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> 

のViewModelコードは

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; 
    } 
} 
です0 MainWindowPropertiesクラスで

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

    public string SelectedProcess { get; set; } 
} 

そして、私のメインウィンドウコンストラクタ内私はlvProcessSelectionからの選択が変更された場合は、次のコードは

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

これを実行されたDataContext

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

を設定していますViewModel内のSelectedProcessを "Expense Reserve Automation"または "Financial Close Status"のいずれかで更新します私は選択を変更したときに、私は新しいメッセージでlbxProgressを更新し、

しかしアプリケーションランタイムとtxtRunTimeを更新することができる午前として 私はDataContextのが私のViewModelのために正しく設定されている知っている(しかし、ContentControlにするためにできない場合があります) lvProcessSelectionでは何も起こりません。 ERAFileSelectの既定のコントロールが残ります。

誰でもこれを解決する方法について正しい方向に私を指摘できますか?

答えて

1

これを解決する方法について誰でも正しい方向に向けることができますか?

INotifyPropertyChangedインターフェイスを実装し、SelectedProcessプロパティが新しい値に設定されるたびに変更通知を上げる必要がありますあなたのMainWindowPropertiesクラス:

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 = "Template"の代わりにProperty = "ContentTemplate"を設定しても効果はありません。コントロールはまだ更新されません – gheff

+0

コンテンツコントロールのコンテンツプロパティを設定する必要があります。 Content = "{Binding}" – Rudra

+0

これは何の違いもありませんでした。残りのバインディングはContentControlの権利ですか?申し訳ありませんが、私はWPFの初心者です。私はあなたの提案を反映するために上記のXAMLを更新しました – gheff

関連する問題